json - gunicorn 日志配置 access_log_format
问题描述
我希望 gunicorn 在我的 docker 容器中记录 JSON。我想在配置文件中使用 --access-logformat 。尝试添加format
或access_log_format
不access_logformat
配置记录器。如何配置 access_log_format 以输出 JSON?
http://docs.gunicorn.org/en/stable/settings.html#access-log-format
gunicorn_logging.conf
[loggers]
keys=root, gunicorn.error, gunicorn.access
[handlers]
keys=console
[formatters]
keys=json
[logger_root]
level=INFO
handlers=console
access_log_format = '{"remote_ip":"%(h)s","request_id":"%({X-Request-Id}i)s","response_code":"%(s)s","request_method":"%(m)s","request_path":"%(U)s","request_querystring":"%(q)s","request_timetaken":"%(D)s","response_length":"%(B)s", "remote_addr": "%(h)s"}'
[logger_gunicorn.error]
level=ERROR
handlers=console
propagate=0
qualname=gunicorn.error
[logger_gunicorn.access]
level=DEBUG
handlers=console
propagate=0
qualname=gunicorn.access
access_log_format = '{"remote_ip":"%(h)s","request_id":"%({X-Request-Id}i)s","response_code":"%(s)s","request_method":"%(m)s","request_path":"%(U)s","request_querystring":"%(q)s","request_timetaken":"%(D)s","response_length":"%(B)s", "remote_addr": "%(h)s"}'
[handler_console]
class=StreamHandler
formatter=json
args=(sys.stdout, )
[formatter_json]
class=utils.jsonlogger.JSONWithTime
utils.jsonlogger:
from pythonjsonlogger import jsonlogger
import time
class JSONWithTime(jsonlogger.JsonFormatter):
def formatTime(self, record, datefmt=None):
ct = self.converter(record.created)
t = time.strftime("%Y-%m-%dT%H:%M:%S", ct)
s = "%s.%03dZ" % (t, record.msecs)
return s
命令行:
gunicorn myapp.wsgi:application --bind 0.0.0.0:8002 --worker-tmp-dir /dev/shm --log-level=DEBUG --timeout 120 --access-logfile=- --error-logfile=- --log-config /app/gunicorn_logging.conf
我想要的是:
{
"stack_info": null,
"level": "INFO",
"timestamp": "2018-02-18T16:20:13.153947Z",
"path": "/usr/local/lib/python3.5/site-packages/gunicorn/glogging.py",
"message": "{\"request\": \"POST /format HTTP/1.1\", \"http_status_code\": \"200\", \"http_request_url\": \"/format\", \"http_query_string\": \"-\", \"http_verb\": \"POST\", \"http_version\": \"1.1\", \"remote_addr\": \"8.8.8.8\"}",
"host": "87eab0ccce6a",
"logger": "gunicorn.access",
"tags": []
}
解决方案
推荐阅读
- ios - 动态调整内部文字大小以适应后动态调整UILabel框架高度
- c++ - 智能指针的铸造
- r - R如何使用带有过滤器或过滤器_的卷曲卷曲?
- json - 当 jsonb 效率更高时,为什么 PostgreSQL 保留 json 字段?
- apache-atlas - can i restore soft deleted entity in apache atlas?
- java - 我无法使用 ip addr 连接 ldap 服务器
- javascript - 使用 forEach 过滤 JSON 对象
- python - 使用一列中的多个值过滤数据框中的值
- google-app-engine - APP Engine:OAuth 客户端被删除?
- react-native - 如何解决 assembleRelease 上的 react-native-immediate-phone-call 错误