supervisord logs don't show my output

三世轮回 提交于 2019-12-01 13:46:21

问题


I have a [program:x] running and it prints / sys.stdout.writes a lot of things. None of which comes up in either in the AUTO childlogdir of [supervisord] or in stdout_logfile of [program:x] Am I missing something?

How do I capture all that is printed or stdout-ed from [program:x] ?

In my program I am explicitly doing both,

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

Relevant supervisord.conf file

[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

回答1:


Python output is buffered. Setting the environment variable PYTHONUNBUFFERED=1 in you supervisord.conf will disable buffering and show log messages sooner:

[program:x]
environment = PYTHONUNBUFFERED=1

or add the -u command-line switch to python command:

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

Alternatively you can flush the sys.stdout handler explicitly:

sys.stdout.flush()

On python 3.3 and up, you can add the flush=True parameter to have the function do this for you:

print(something, flush=True)



回答2:


You can run your program like this:

python -u file.py

this will produce unbuffered output




回答3:


If you have python based script that you can't or don't want to change to flush output on a regular base then you can use unbuffer from the Expect package.

For a Django application in a docker container I've lately used it like that (from a shell script run from supervisord):

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


来源:https://stackoverflow.com/questions/13934801/supervisord-logs-dont-show-my-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!