问题描述
我正在尝试做与此处解释的非常相似的事情: https://sebest.github.io/post/protips-using-gunicorn-inside-a-docker-image/
I am trying to do something very similar to what's explained here: https://sebest.github.io/post/protips-using-gunicorn-inside-a-docker-image/
我想让我的Flask应用程序+ gunicorn都输出JSON格式的日志记录.我可以在Flask应用程序中使用它,但似乎无法在gunicorn中使用它.
I want to get my Flask app + gunicorn both outputting JSON formatted logging. I've got this working for the Flask app, but I can't seem to get it working with gunicorn.
这是我当前的输出:
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger pin code: 317-187-130
192.168.99.1 - - [12/Jan/2016 20:09:29] "GET /nothing HTTP/1.1" 404 -
{"asctime": "2016-01-12 20:20:25,541", "levelname": "WARNING", "module": "app", "funcName": "err", "lineno": 17, "message": "will assert false..."}
192.168.99.1 - - [12/Jan/2016 20:20:25] "GET /err HTTP/1.1" 500 -
以{"asctime":
开头的行是代码app.logger.warning('will assert false...')
的输出,该代码已正确记录为JSON.耶.以192.168.99.1
开头的行是从我的gunicorn WSGI服务器输出的,令人沮丧的是,它们不是JSON格式的.
The line that begins {"asctime":
is the output of the code app.logger.warning('will assert false...')
which is being correctly logged as JSON. Yay. The lines that begin with 192.168.99.1
are output from my gunicorn WSGI server and, frustratingly, they are not JSON formatted.
这是我用来启动gunicorn的命令:
Here's the command I am using to start gunicorn:
gunicorn --log-config gunicorn_logging.conf -c gunicorn_config.py api.app:app
gunicorn_logging.conf
文件包含的位置:
[loggers]
keys=root, gunicorn.error
[handlers]
keys=console
[formatters]
keys=json
[logger_root]
level=INFO
handlers=console
[logger_gunicorn.error]
level=ERROR
handlers=console
propagate=0
qualname=gunicorn.error
[handler_console]
class=StreamHandler
formatter=json
args=(sys.stdout, )
[formatter_json]
class=jsonlogging.JSONFormatter
并且文件gunicorn_config.py
包含:
import os
import multiprocessing
addr = os.environ.get('HTTP_ADDR', '0.0.0.0')
port = os.environ.get('HTTP_PORT', '5000')
loglevel = os.environ.get('LOG_LEVEL', 'info')
bind = '{0}:{1}'.format(addr, port)
workers = multiprocessing.cpu_count() * 5 + 1
worker_class = 'gevent'
timeout = 0
这是pip freeze
的输出:
aniso8601==1.1.0
coverage==4.0.3
flake8==2.5.1
Flask==0.10.1
Flask-MySQLdb==0.2.0
Flask-RESTful==0.3.5
Flask-Script==2.0.5
gevent==1.1rc3
greenlet==0.4.9
gunicorn==19.4.5
itsdangerous==0.24
Jinja2==2.8
json-logging-py==0.2
MarkupSafe==0.23
marshmallow==2.4.2
mccabe==0.3.1
mysqlclient==1.3.7
nose==1.3.7
pep8==1.5.7
pyflakes==1.0.0
python-dateutil==2.4.2
python-json-logger==0.1.4
pytz==2015.7
six==1.10.0
SQLAlchemy==1.0.11
Werkzeug==0.11.3
推荐答案
以获取访问日志以及您的配置文件应如下所示:
to get the access log as well your config file should look something like this:
[loggers]
keys=root, gunicorn.error, gunicorn.access
...
[logger_gunicorn.access]
level=(DEBUG|INFO|ERROR)
handlers=console
propagate=0
qualname=gunicorn.access
访问记录在INFO日志级别中,因此,如果要显示它们,请确保已将"logger_gunicorn.access"标记内的日志级别设置为至少INFO
The accesses are logged in the INFO log level, so if you want to show them make sure you have the log level within the "logger_gunicorn.access" tag set to at least INFO
这篇关于使用Flask和Gunicorn进行JSON格式的日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!