监督日志不显示我的输出
问题内容:
我有一个[程序:x]运行,它打印/
sys.stdout.write很多东西。在[supervisord]的AUTO子日志目录或[program:x]的stdout_logfile中都没有出现任何问题吗?
如何捕获[program:x]中所有已打印或标准输出的内容?
在我的程序中,我明确地同时做了这两项工作,
print "something"
sys.stdout.write("something")
相关的supervisor.conf文件
[supervisord]
childlogdir = %(here)s/../logs/supervisord/
logfile = %(here)s/../logs/supervisord/supervisord.log
logfile_maxbytes = 100MB
logfile_backups = 10
loglevel = info
pidfile = %(here)s/../logs/supervisord/supervisord.pid
umask = 022
nodaemon = false
nocleanup = false
[program:x]
directory = %(here)s/../
command = python file.py
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile = /appropriate-path/to/access.log
问题答案:
Python输出已缓冲。在您中设置环境变量PYTHONUNBUFFERED=1
supervisord.conf
将禁用缓冲并尽快显示日志消息:
[program:x]
environment = PYTHONUNBUFFERED=1
或将-u
命令行开关添加到python
命令:
[program:x]
command = python -u file.py
或者,您可以sys.stdout
显式刷新处理程序:
sys.stdout.flush()
在python 3.3及更高版本上,您可以添加flush=True
参数以使函数为您执行此操作:
print(something, flush=True)