Nicer gunicorn json log message formatting
MartinThoma opened this issue · 2 comments
MartinThoma commented
I use pythonjsonlogger
for gunicorn by setting --log-config gunicorn_logging.conf
when calling gunicorn.
I get a JSON log message:
{"asctime": "2020-12-07 02:36:29,798", \
"name": "gunicorn.access", \
"levelname": "INFO", \
"message": "127.0.0.1 - - [07/Dec/2020:02:36:29 +0100] \"GET /spec HTTP/1.1\" 200 104065 \"http://localhost:9000/api/docs\" \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36\""}
What I don't like is that the structure is still hidden in the message and that the time is still in the message. I would like to get to this:
{"asctime": "2020-12-07 02:36:29,798", \
"name": "gunicorn.access", \
"levelname": "INFO", \
"requester": "127.0.0.1", \
"requested": "GET /spec", \
"http_refererr": "http://localhost:9000/api/docs", \
"response_code": 200, \
"response_size": 104065, \
"browser": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36", \
"message": "GET /spec"}
Do you know how?
gunicorn_logging.config
My gunicorn_logging.config
looks like this:
[loggers]
keys=root, gunicorn.error, gunicorn.access
[handlers]
keys=console
[formatters]
keys=json
[logger_root]
level=INFO
handlers=console
[logger_gunicorn.error]
level=ERROR
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=pythonjsonlogger.jsonlogger.JsonFormatter
format=%(asctime)s %(name)s %(levelname)s %(message)s
MartinThoma commented
I've tried the ones from access_log_format
on page 18, but the values were always null
will7200 commented
Posted a solution over at https://stackoverflow.com/a/70511781/8190553