【问题标题】:Python logging with Supervisor使用 Supervisor 进行 Python 日志记录
【发布时间】:2016-03-25 16:53:00
【问题描述】:

我目前正在使用 supervisor 来轻松地监控和守护一些 python 脚本。但是,主管似乎无法正确登录。 我正在执行的脚本就这么简单:

#!/usr/bin/env python
import pushybullet as pb
import sys, time, logging

# INIT LOGGING
logging.basicConfig(format='%(asctime)s @%(name)s [%(levelname)s]:    %(message)s', level = logging.DEBUG)

if __name__ == '__main__':
    try:
            logging.info('Custom service started')
            while True:
                    #here for the sake of example, actually doing real stuff here
                    time.sleep(2)
    finally:
            logging.info('Custom service stopped')

这里是对应的conf文件:

[program:myscript]
directory=/home/pi/Documents/Scripts_py
command=python -u myscript.py
autostart=true
autorestart=true

所以我根据谷歌上的许多研究测试了很多东西。 通过打印替换日志行然后刷新标准输出确实有效;与启动脚本的 -u 选项相同。但是 print 不能满足我的需要,Python 的 logging 模块可以。因此,我尝试在每个日志记录行之后刷新,并在无缓冲模式中启动脚本,但什么也没有出现!

提前感谢您的帮助!

【问题讨论】:

  • 你试过在你的conf文件中指定你想要的日志文件吗?例如:stdout_logfile=/var/log/supervisor/%(program_name)s.log
  • 这不是为了指定我们希望主管在哪里输出它自己的程序日志吗?如果是,这对我没有帮助,因为我面临的问题是我的脚本日志根本没有被主管捕获
  • 好的,我试了一下,解决了,谢谢!
  • 是的,这是正确的(关于文件访问权限),即使您可以在主管 conf 文件中指定 uid:user=youruser

标签: python logging supervisord


【解决方案1】:

感谢 Pedro Rodrigues,我找到了。对于像我这样苦苦挣扎的人,只要知道你只是创建一个这样的文件处理程序:

fh = logging.FileHandler('/var/log/supervisor/your_program_name.log') 

并添加到您的程序配置文件中:

stdout_logfile=/var/log/supervisor/%(program_name)s.log 

脚本将写入日志,然后由主管读取,在 Web 界面上可见。如果您尝试在没有主管的情况下启动脚本,它可能会产生的一个问题是权限被拒绝。

【讨论】:

    猜你喜欢
    • 2014-11-06
    • 1970-01-01
    • 2010-12-11
    • 2018-11-19
    • 1970-01-01
    • 2012-05-11
    • 2011-03-01
    • 1970-01-01
    相关资源
    最近更新 更多