首页 > 解决方案 > UWSGI 不会将应用程序标准输出重定向到标准输出,仅重定向到文件

问题描述

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

但是当我logtouwsgi.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

启用后,日志文件将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)

一旦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)

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

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

  1. 由于 uWSGI 在带有 Supervisor 的 Docker 容器中运行,我需要让 supervisor 将 uwsgi 的标准输出重定向到它的标准输出。
    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

虽然这适用于其他应用程序(如 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

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

标签: pythondockerflaskuwsgi

解决方案


推荐阅读