UWSGI 不会将应用程序 stdout 重定向到 stdout,仅重定向到文件

Zol*_*dor 12 python flask uwsgi docker

我有一个在容器内运行的 Flask 应用程序,我在其中设置了日志记录StreamHandler(),因此日志被发送到标准输出。
当我的uwsgi.ini文件包含将日志重定向到文件的语句(通过使用logto)时,来自应用程序的日志可在与 uWSGI 日志混合的日志文件中使用(正如预期的那样)。

但是当我从logto-uwsgi.ini因为我希望将这些日志发送到 Docker 容器标准输出时,只有 uWSGI 日志在 docker 容器日志中可见,而应用程序日志则不可见。(uWSGI 日志甚至之前就在那里)

uwsgi.ini:

[uwsgi]
base = /app_home/app
wsgi-file = /app_home/app/wsgi.py
callable = app
socket = /tmp/uwsgi.sock
chmod-socket    = 666
# Log directory - we needed this to be turned off, so the logs are sent to STDOUT
# logto = /var/log/uwsgi/app.log
vacuum = true
master = true
processes = 3
enable-threads = true
uid = app
gid = app
master-fifo = /tmp/fifo0
master-fifo = /tmp/fifo1

chdir = /app_home/app
Run Code Online (Sandbox Code Playgroud)

启用后logto,日志文件将包含应用程序的日志(理应如此):

[2019-03-05 17:19:05,415] INFO in __init__: Initial starting of app...
2019-03-05 17:19:05,415 (9) - INFO - Initial starting of app...- [in ./app/__init__.py:128, function:create_app]
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x1ad49b0 pid: 9 (default app)
*** uWSGI is running in multiple interpreter mode ***
gracefully (RE)spawned uWSGI master process (pid: 9)
spawned uWSGI worker 1 (pid: 32, cores: 1)
Run Code Online (Sandbox Code Playgroud)

一旦logto禁用,则不会有日志文件(如预期),但 Docker 容器日志中也不会记录任何应用程序。docker 容器看起来和以前一模一样:

2019-03-05T22:19:09.956784133Z 2019-03-05 17:19:09,956 CRIT Supervisor running as root (no user in config file)
2019-03-05T22:19:09.959701644Z 2019-03-05 17:19:09,959 INFO supervisord started with pid 1
2019-03-05T22:19:10.961366502Z 2019-03-05 17:19:10,961 INFO spawned: 'nginx' with pid 9
2019-03-05T22:19:10.963312945Z 2019-03-05 17:19:10,962 INFO spawned: 'uwsgi' with pid 10
2019-03-05T22:19:12.928470278Z 2019-03-05 17:19:12,928 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2019-03-05T22:19:12.928498809Z 2019-03-05 17:19:12,928 INFO success: uwsgi entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
Run Code Online (Sandbox Code Playgroud)

uWSGI文档显示日志默认发送到stdout/stderr(参见https://uwsgi-docs.readthedocs.io/en/latest/Logging.html),所以我真的不明白应用程序日志的原因不会使用 uWSGI 自己的日志发送到 stdout,而是发送到带有logto.

编辑(已解决)
所以事实证明有两个问题:

  1. 由于uWSGI与Supervisor一起在Docker容器中运行,我需要让supervisor将uwsgi的stdout重定向到它的stdout。
    Supervisord.conf:
[program:uwsgi]
command=/usr/local/bin/uwsgi --ini /app_home/app/uwsgi.ini --uid app --gid app --log-master
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
Run Code Online (Sandbox Code Playgroud)

虽然这已经适用于其他应用程序(如 NGinx),但对于 uWSGI 来说还不够,请参阅步骤 #2:

  1. --log-master必须向 uWSGI 添加一个特殊标志 ( ),以将日志记录委托给主进程:
[program:uwsgi]
command=/usr/local/bin/uwsgi --ini /app_home/app/uwsgi.ini --uid app --gid app --log-master
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
Run Code Online (Sandbox Code Playgroud)

哇啦,来自 Flask 应用程序的日志在 Docker 容器日志中可见。

vin*_*zee 1

有两个问题:

由于uWSGI与Supervisor一起在Docker容器中运行,因此您需要让supervisor将uwsgi的stdout重定向到其stdout。

supervisord.conf

[program:uwsgi]
command=/usr/local/bin/uwsgi --ini /app_home/app/uwsgi.ini --uid app --gid app --log-master
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
Run Code Online (Sandbox Code Playgroud)

虽然这已经适用于其他应用程序(例如 nginx),但对于 uWSGI 来说还不够,请参阅步骤 #2:

--log-master需要向 uWSGI 添加一个特殊标志 ( ) 以将日志记录委托给主进程:

[program:uwsgi]
command=/usr/local/bin/uwsgi --ini /app_home/app/uwsgi.ini --uid app --gid app --log-master
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
Run Code Online (Sandbox Code Playgroud)

然后,来自 Flask 应用程序的日志将在 Docker 容器日志中可见。


此答案是作为对问题UWSGI does not redirect application stdout to stdout, only to file by OP Zoltan Fedor under CC BY-SA 4.0 的编辑发布的。