【问题标题】:supervisord logs don't show my outputsupervisord 日志不显示我的输出
【发布时间】:2012-12-05 18:24:53
【问题描述】:

我有一个 [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

【问题讨论】:

  • 您确定程序打印到stdout 而不是stderr
  • 用更多信息编辑了问题。当我执行 sys.stdout.write 时,它​​应该打印到标准输出
  • [program:..] 的supervisord 配置是什么?哦,试试sys.stdout.flush() 以确保它不是缓冲区。
  • 我把整个conf文件放进去。另外,我尝试在 file.py 的末尾添加 sys.stdout.flush(),但没有成功。
  • 如果你检查你的supervisord日志文件,你真的看到你的进程已经启动了吗?如果程序从未运行过,它就不能输出任何东西。

标签: python logging supervisord


【解决方案1】:

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】:

    你可以这样运行你的程序:

    python -u file.py
    

    这将产生无缓冲的输出

    【讨论】:

    • 正确 (15char)
    【解决方案3】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2015-10-11
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 2011-05-12
      • 2017-12-06
      相关资源
      最近更新 更多