supervisord日志不显示我的输出

时间:2012-12-18 14:11:52

标签: python logging supervisord

我有一个[program:x]正在运行,它打印/ sys.stdout.writes很多东西。其中没有出现在[supervisord]的AUTO childlogdir或[program:x]的stdout_logfile中 我错过了什么吗?

如何从[program:x]中捕获所有打印或标注的内容?

在我的程序中,我明确地同时做了两件事,

print "something"
sys.stdout.write("something") 

相关的supervisord.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

3 个答案:

答案 0 :(得分:43)

您可以像这样运行您的程序:

python -u file.py

这将产生无缓冲的输出

答案 1 :(得分:30)

Python输出被缓冲。在supervisord.conf中设置environment variable PYTHONUNBUFFERED=1将禁用缓冲并更快地显示日志消息:

[program:x]
environment = PYTHONUNBUFFERED=1

或将-u command-line switch添加到python命令:

[program:x]
command = python -u file.py

或者,您可以明确刷新sys.stdout处理程序:

sys.stdout.flush()

在python 3.3及更高版本中,您可以添加flush=True参数以让函数为您执行此操作:

print(something, flush=True)

答案 2 :(得分:1)

如果您有基于python的脚本,您不能或不想更改为常规基础上的刷新输出,那么您可以使用Expect包中的unbuffer

对于docker容器中的Django应用程序,我最近使用它(来自从supervisord运行的shell脚本):

unbuffer python -u manage.py runserver 0.0.0.0:11000 2>&1 >> /var/log/django.log