我打算在 kubernetes 上通过 gunicorn 运行烧瓶。为了正确记录日志,我想在 json 中输出我的所有日志。
目前我正在使用 minikube 和https://github.com/inovex/kubernetes-logging进行测试,以流畅地收集日志。
由于使用 Flask 和 gunicorn 进行 JSON 格式的日志记录,我设法正确格式化了错误日志(回溯)
我仍在为访问日志格式而苦苦挣扎。我指定了以下 gunicorn 访问日志格式:
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"}'
Run Code Online (Sandbox Code Playgroud)
并且生成的日志是json格式的。但是消息部分(基于 access_log_format 的格式)现在包含转义的双引号,并且不会被 fluentd / ELK 解析到它的各个字段中
{"tags": [], "timestamp": "2017-12-07T11:50:20.362559Z", "level": "INFO", "host": "ubuntu", "path": "/usr/local/lib/python2.7/dist-packages/gunicorn/glogging.py", "message": "{\"remote_ip\":\"127.0.0.1\",\"request_id\":\"-\",\"response_code\":\"200\",\"request_method\":\"GET\",\"request_path\":\"/v1/records\",\"request_querystring\":\"\",\"request_timetaken\":\"19040\",\"response_length\":\"20\"}", "logger": "gunicorn.access"}
Run Code Online (Sandbox Code Playgroud)
谢谢 Jpw