我正在使用这些选项(以及其他选项)围绕 Python Flask webapp 运行 uwsgi,以在标准输出上获取 JSON 编码的日志记录:
fmt=$'{"timestamp": "${strftime:%FT%TZ}", "level": "DEBUG", "name": "uwsgi", "message": "${msg}"}\n'
uwsgi --env="TZ=UTC" --log-encoder="json ${fmt}" --logformat="%(status) [%(msecs)ms] %(method) %(uri)"
Run Code Online (Sandbox Code Playgroud)
这很好地编码了来自 uwsgi 的标准输出,但不幸的是也编码了来自我的应用程序的日志记录,它已经是 JSON 格式,所以我得到如下内容:
{"timestamp": "2017-10-02T22:48:11Z", "level": "DEBUG", "name": "uwsgi", "message": "spawned uWSGI http 1 (pid: 75298)"}
{"timestamp": "2017-10-02T22:48:15Z", "level": "DEBUG", "name": "uwsgi", "message": "{\"timestamp\": \"2017-10-02T22:48:15.200Z\", \"message\": \"Descriptor requested\", \"request\": \"c6b08680-a7c3-11e7-9495-186590cba8eb\", \"name\": \"myapp.main\", \"level\": \"INFO\"}"}
{"timestamp": "2017-10-02T22:48:15Z", "level": "DEBUG", "name": "uwsgi", "message": "200 [11ms] GET /descriptor.json"}
Run Code Online (Sandbox Code Playgroud)
中间记录与 uwsgi 的其他输出使用相同的 JSON 编码。
如何避免 Flask 应用程序的输出被编码,但保持 uwsgi …