python - UWSGI 不会将应用程序标准输出重定向到标准输出,仅重定向到文件
问题描述
我有一个在容器内运行的 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
启用后,日志文件将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
.
编辑(已解决):
所以事实证明有两个问题:
- 由于 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:
--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 容器日志中可见。
解决方案
推荐阅读
- cloud-init - cloud-init 从安全主机获取脚本
- python - 我可以在不先绘制直方图的情况下绘制正态概率分布吗?
- spring - 如何配置 Spring MVC 以防止“基于路径的漏洞”
- typescript - 类型上不存在开玩笑的打字稿属性模拟
- google-apps-script - 从 Google 表格创建 Google 日历条目
- ios - 应用程序进入后台时 POST 请求错误
- python - Jupyter Notebook 中的 Flask 应用程序
- c++ - 使用参数化构造函数 C++ 动态分配对象数组
- codeblocks - 使用 sprintf() 时对 __mulhi3 的未定义引用 - Windows 上 Code::Blocks 中的 AVR GCC
- python - 使用 PyGame 中的操纵杆模块