使用默认的日志配置,Gunicorn将替换模板日志消息中的侦听ip/端口和工作者pid之类的值。在添加自定义日志配置以登录json之后,它不再执行替换操作。
我不知道在自定义配置中缺少什么来恢复该行为。
下面是现在发生的情况:
{"log_level": "INFO", "msg": "Starting gunicorn %s", "timestamp": "2022-10-11T10:47:50.705102Z"}
{"log_level": "INFO", "msg": "Listening at: %s (%s)", "timestamp": "2022-10-11T10:47:50.705515Z"}
{"log_level": "INFO", "msg": "Using worker: %s", "timestamp": "2022-10-11T10:47:50.705545Z"}
{"log_level": "INFO", "msg": "Booting worker with pid: %s", "timestamp": "2022-10-11T10:47:50.712564Z"}以下是我以前的经历:
[2022-10-11 11:49:09 +0100] [71004] [INFO] Starting gunicorn 20.1.0
[2022-10-11 11:49:09 +0100] [71004] [INFO] Listening at: http://0.0.0.0:5002 (71004)
[2022-10-11 11:49:09 +0100] [71004] [INFO] Using worker: sync
[2022-10-11 11:49:09 +0100] [71005] [INFO] Booting worker with pid: 71005下面是日志配置文件:
[loggers]
keys=root, gunicorn.error, gunicorn.access
[handlers]
keys=console
[formatters]
keys=json
[logger_root]
level=INFO
handlers=console
[logger_gunicorn.error]
level=INFO
handlers=console
propagate=0
qualname=gunicorn.error
[logger_gunicorn.access]
level=INFO
handlers=console
propagate=0
qualname=gunicorn.access
[handler_console]
class=StreamHandler
formatter=json
args=(sys.stdout, )
[formatter_json]
class=src.logging_helpers.json_log_formatter.GunicornLogFormatter
format='%(log_level)s %(msg)s %(timestamp)s'下面是其中提到的格式化程序:
class GunicornLogFormatter(JsonFormatter):
def add_fields(self, log_record, record, message_dict):
"""
This method allows us to inject gunicorn's args as fields for the formatter
"""
super(GunicornLogFormatter, self).add_fields(log_record, record, message_dict)
now = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ')
log_record['timestamp'] = now
if 'r' in record.args:
log_record['request'] = record.args.get('r')
log_record['msg'] = None
if 's' in record.args:
log_record['status_code'] = record.args.get('s')
if 'level' in log_record:
log_record['log_level'] = log_record['level'].upper()
else:
log_record['log_level'] = record.levelname任何帮助都是非常感谢的。
发布于 2022-10-12 08:52:06
我发现实际上提供了带有替换值的消息,但在日志记录消息中的" message“键下。在"msg“下,我们得到了没有替换值的消息,因此,我得到了这个结果。
作为解决办法,我在配置中添加了一个新的格式化程序,指向相同的格式化程序类,但使用不同的模式,以访问"message“键。
然后,在类代码本身中,我在"msg“和"message”之间切换。
这是最后的结果:
配置文件:
[loggers]
keys=root, gunicorn.access, gunicorn.error
[handlers]
keys=console,server
[formatters]
keys=json, server
[logger_root]
level=INFO
handlers=console
[logger_gunicorn.access]
level=INFO
handlers=console
propagate=0
qualname=gunicorn.access
[logger_gunicorn.error]
level=INFO
handlers=server
propagate=0
qualname=gunicorn.error
[handler_console]
class=StreamHandler
formatter=json
args=(sys.stdout, )
[handler_server]
class=StreamHandler
formatter=server
args=(sys.stdout, )
[formatter_json]
class=src.logging.json_log_formatter.GunicornLogFormatter
format='%(log_level)s %(msg)s %(timestamp)s'
[formatter_server]
format='%(log_level)s %(message)s %(msg)s %(timestamp)s'
rename_fields=(message:msg)
class=src.logging.json_log_formatter.GunicornLogFormatter格式化程序类:
from datetime import datetime
from pythonjsonlogger.jsonlogger import JsonFormatter
class GunicornLogFormatter(JsonFormatter):
def add_fields(self, log_record, record, message_dict):
super(GunicornLogFormatter, self).add_fields(log_record, record, message_dict)
now = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ')
log_record['timestamp'] = now
# this 'if-clause' is necessary for us to handle gunicorn's logging in our required format.
if 'message' in log_record:
log_record['msg'] = log_record['message']
log_record.pop('message')
# here we do some name normalization with what we want and gunicorn default logging provide us
if 'r' in record.args:
log_record['request'] = record.args.get('r')
log_record['msg'] = None
if 's' in record.args:
log_record['status_code'] = record.args.get('s')
if 'level' in log_record:
log_record['log_level'] = log_record['level'].upper()
else:
log_record['log_level'] = record.levelnamehttps://stackoverflow.com/questions/74026946
复制相似问题