Python 如何配置gunicorn以使用一致的错误日志格式?

Python 如何配置gunicorn以使用一致的错误日志格式?,python,logging,gunicorn,Python,Logging,Gunicorn,我正在Python Flask应用程序前使用Gunicorn。运行gunicorn时,我可以使用--access log format命令行参数配置访问日志格式。但是我不知道如何配置错误日志 我可以使用默认格式,但不一致。看起来Gunicorn状态消息有一种格式,但应用程序异常有不同的格式。这使得使用日志聚合变得困难 例如,这里有一些来自Gunicorn错误日志的消息。前几行的格式与异常行不同。事件日期时间格式不同 [2017-07-13 16:33:24 +0000] [15] [INFO]

我正在Python Flask应用程序前使用Gunicorn。运行
gunicorn
时,我可以使用
--access log format
命令行参数配置访问日志格式。但是我不知道如何配置错误日志

我可以使用默认格式,但不一致。看起来Gunicorn状态消息有一种格式,但应用程序异常有不同的格式。这使得使用日志聚合变得困难

例如,这里有一些来自Gunicorn错误日志的消息。前几行的格式与异常行不同。事件日期时间格式不同

[2017-07-13 16:33:24 +0000] [15] [INFO] Booting worker with pid: 15
[2017-07-13 16:33:24 +0000] [16] [INFO] Booting worker with pid: 16
[2017-07-13 16:33:24 +0000] [17] [INFO] Booting worker with pid: 17
[2017-07-13 16:33:24 +0000] [18] [INFO] Booting worker with pid: 18
[2017-07-13 18:31:11,580] ERROR in app: Exception on /api/users [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
...

配置Gunicorn以对其错误日志使用一致格式的最佳方法是什么?

使用此日志配置文件,我可以更改错误日志格式

[loggers]
keys=root, gunicorn.error

[handlers]
keys=error_console

[formatters]
keys=generic

[logger_root]
level=INFO
handlers=error_console

[logger_gunicorn.error]
level=INFO
handlers=error_console
propagate=0
qualname=gunicorn.error

[handler_error_console]
class=StreamHandler
formatter=generic
args=(sys.stderr, )

[formatter_generic]
format=%(asctime)s %(levelname)-5s [%(module)s] ~ %(message)s
datefmt=%Y-%m-%d %H:%M:%S %Z
class=logging.Formatter
关键是覆盖
gunicorn.error
logger配置,上面截取的就是这样做的


请注意
propagate=0
字段,否则日志消息将打印两次很重要(gunicorn始终保留默认的日志配置)。

有关用法的说明,我仍然必须指定
errorlog='gunicorn error.log'
并使用-
gunicorn wsgi:application--config../deploy/gunicorn.conf--log config../deploy/gunicorn log.conf运行它