使用Supervisor进行Python日志记录 [英] Python logging with Supervisor

查看:1124
本文介绍了使用Supervisor进行Python日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用超级用户来轻松监控和守护某些python脚本.但是,主管似乎无法正确登录. 我正在执行的脚本是如此简单:

I'm currently using supervisor to monit and daemonize some python scripts easily. However, it seems that supervisor won't log properly. The scripts I'm executing is as simple as this :

#!/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文件:

And here is the corresponding conf file :

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

因此,我根据Google的许多研究成果对许多事物进行了测试. 通过打印替换记录行,然后刷新stdout确实可行;与-u选项相同以启动脚本.但是打印不足以满足我的需求,Python的日志记录模块可以满足要求.因此,我尝试在每条记录行之后刷新,并以无缓冲的mod启动脚本,但是什么也没有出现!

So I've tested many things based on many researched on google. Replacing logging lines by print and then flushing stdout indeeds works; same with -u option to launche the script. But print is not adequate for my needs, Python's logging module is. So I tried to flush after each logging line, and to launch the script in unbuffered mod, but nothing appears!

在此先感谢您的帮助!

Thanks in advance for any help!

推荐答案

感谢我发现了Pedro Rodrigues.对于像我这样挣扎的任何人,都应该知道您只是创建了一个像这样的fileHandler:

Thanks to Pedro Rodrigues, I've found. For anyone struggling like me, well just know that you just create a fileHandler like this :

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

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

And add to your program conf file :

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

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

The scripts will write in the log, then read by supervisor, visible on the web interface. One issue it might create is a permission denied if you try to start your script without supervisor.

这篇关于使用Supervisor进行Python日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆