Python 监督日志不';不显示我的输出
我有一个[program:x]在运行,它打印/sys.stdout.write很多东西。在[supervisord]的AUTO-childlogdir或[program:x]的stdout_-logfile中都没有出现 我错过什么了吗 如何捕获从[program:x]打印或打印的所有内容 在我的程序中,我明确地做到了这两个方面Python 监督日志不';不显示我的输出,python,logging,supervisord,Python,Logging,Supervisord,我有一个[program:x]在运行,它打印/sys.stdout.write很多东西。在[supervisord]的AUTO-childlogdir或[program:x]的stdout_-logfile中都没有出现 我错过什么了吗 如何捕获从[program:x]打印或打印的所有内容 在我的程序中,我明确地做到了这两个方面 print "something" sys.stdout.write("something") 相关supervisord.conf文件 [supervisord]
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
Python输出是缓冲的。在您的
supervisord.conf
中设置将禁用缓冲并更快地显示日志消息:
[program:x]
environment = PYTHONUNBUFFERED=1
或者添加topython
命令:
[program:x]
command = python -u file.py
或者,您可以显式刷新sys.stdout
处理程序:
sys.stdout.flush()
在python 3.3及更高版本上,可以添加flush=True
参数,让函数为您执行以下操作:
print(something, flush=True)
您可以这样运行程序:
python -u file.py
这将产生无缓冲的输出如果您有基于python的脚本,您不能或不想更改为定期刷新输出,那么您可以使用Expect包 对于docker容器中的Django应用程序,我最近这样使用它(从supervisord运行的shell脚本):
您确定该程序打印到
stdout
,而不是stderr
?编辑了包含更多信息的问题。当我执行sys.stdout.write时,它应该打印到stdou,并且该[程序:…]
的监控命令配置是什么?哦,试一下sys.stdout.flush(),确保它不是缓冲区。我把整个conf文件都放进去了。此外,我还尝试在file.py的末尾添加一个sys.stdout.flush()
,但没有成功。如果您检查您的supervisord日志文件,您是否确实看到您的进程已启动?如果程序从未运行过,则无法输出任何内容。正确(15char)
unbuffer python -u manage.py runserver 0.0.0.0:11000 2>&1 >> /var/log/django.log