一尘不染

Docker日志中的Python异常标记为流:stdout

docker

我想解析并处理docker容器中的所有错误,但是当我期望stderr时,将python异常标记为stdout。

例如简单的 app.py

raise Exception("!")

然后我在docker容器中运行此文件。但是在 /var/lib/docker/containers/…/…-json.log中

{"log":"Traceback (most recent call last):\n","stream":"stdout","time":"2015-06-17T23:10:01.58636849Z"}
{"log":"  File \"/var/app.py\", line 1, in \u003cmodule\u003e\n","stream":"stdout","time":"2015-06-17T23:10:01.586581081Z"}
{"log":"    raise Exception(\"!\")\n","stream":"stdout","time":"2015-06-17T23:10:01.586842665Z"}
{"log":"Exception: !\n","stream":"stdout","time":"2015-06-17T23:10:01.587373678Z"}

阅读 506

收藏
2020-06-17

共1个答案

一尘不染

我有一个误解。我认为Docker CLI的命令不会影响主日志(/var/lib/docker/containers/…/…-json.log)

但在以下情况下:

泊坞窗运行-it my_python python /var/app.py

json.log内容:

{"log":"Traceback (most recent call last):\n","stream":"stdout","time":"2015-06-18T10:02:55.842010241Z"}
{"log":"  File \"/var/app.py\", line 1, in \u003cmodule\u003e\n","stream":"stdout","time":"2015-06-18T10:02:55.842252975Z"}
{"log":"    raise Exception(\"error\")\n","stream":"stdout","time":"2015-06-18T10:02:55.842423153Z"}
{"log":"Exception: error\n","stream":"stdout","time":"2015-06-18T10:02:55.842754372Z"}

但是,如果我在后台运行容器,流将成为stderr:

docker运行-d my_python python /var/app.py

{"log":"Traceback (most recent call last):\n","stream":"stderr","time":"2015-06-18T10:02:18.905673576Z"}
{"log":"  File \"/var/app.py\", line 1, in \u003cmodule\u003e\n","stream":"stderr","time":"2015-06-18T10:02:18.90575399Z"}
{"log":"    raise Exception(\"error\")\n","stream":"stderr","time":"2015-06-18T10:02:18.905802834Z"}
{"log":"Exception: error\n","stream":"stderr","time":"2015-06-18T10:02:18.90616668Z"}

我认为这种行为是隐性的。

2020-06-17