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

时间:2021-01-29 18:13:25

I am using Gunicorn in front of a Python Flask app. I am able to configure the access log format using the --access-log-format command line parameter when I run gunicorn. But I can't figure out how to configure the error logs.

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

I would be fine with the default format, except it's not consistent. It looks like Gunicorn status messages have one format, but application exceptions have a different format. This is making it difficult to use log aggregation.

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

For example, here a few messages from Gunicorn's error log. The first few lines have a different format than the exception line. Event the datetime format is different.

例如,这里有一些来自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)
...

What's the best way to configure Gunicorn to use consistent formatting for its error logs?

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

1 个解决方案

#1


0  

Using this logging config file, I was able to change the error log format

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

[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

The key is to overwrite the gunicorn.error logger config, and the snipped above does exactly that.

关键是要改写“gunicorn”。错误日志配置,并且上面的剪断是正确的。

Note the propagate=0 field, it is important otherwise your log messages will be printed twice (gunicorn always keeps the default logging config).

注意传播=0字段,否则日志消息将被打印两次(gunicorn总是保存默认日志配置)。

#1


0  

Using this logging config file, I was able to change the error log format

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

[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

The key is to overwrite the gunicorn.error logger config, and the snipped above does exactly that.

关键是要改写“gunicorn”。错误日志配置,并且上面的剪断是正确的。

Note the propagate=0 field, it is important otherwise your log messages will be printed twice (gunicorn always keeps the default logging config).

注意传播=0字段,否则日志消息将被打印两次(gunicorn总是保存默认日志配置)。