Python 使用 Supervisor 记录
Python logging with Supervisor
我目前正在使用 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')
这是相应的配置文件:
[program:myscript]
directory=/home/pi/Documents/Scripts_py
command=python -u myscript.py
autostart=true
autorestart=true
所以我根据对 google 的许多研究测试了很多东西。
通过打印替换日志记录行,然后刷新标准输出确实有效;与启动脚本的 -u 选项相同。但是 print 不能满足我的需求,Python 的日志记录 module 可以。所以我尝试在每个日志行之后刷新,并在无缓冲 mod 中启动脚本,但什么也没有出现!
在此先感谢您的帮助!
感谢 Pedro Rodrigues,我找到了。对于像我一样苦苦挣扎的任何人,只要知道您只是像这样创建一个 fileHandler 即可:
fh = logging.FileHandler('/var/log/supervisor/your_program_name.log')
并添加到您的程序 conf 文件中:
stdout_logfile=/var/log/supervisor/%(program_name)s.log
脚本将写入日志,然后由管理员读取,在 Web 界面上可见。如果您尝试在没有主管的情况下启动脚本,它可能会产生一个问题,即权限被拒绝。
我目前正在使用 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')
这是相应的配置文件:
[program:myscript]
directory=/home/pi/Documents/Scripts_py
command=python -u myscript.py
autostart=true
autorestart=true
所以我根据对 google 的许多研究测试了很多东西。 通过打印替换日志记录行,然后刷新标准输出确实有效;与启动脚本的 -u 选项相同。但是 print 不能满足我的需求,Python 的日志记录 module 可以。所以我尝试在每个日志行之后刷新,并在无缓冲 mod 中启动脚本,但什么也没有出现!
在此先感谢您的帮助!
感谢 Pedro Rodrigues,我找到了。对于像我一样苦苦挣扎的任何人,只要知道您只是像这样创建一个 fileHandler 即可:
fh = logging.FileHandler('/var/log/supervisor/your_program_name.log')
并添加到您的程序 conf 文件中:
stdout_logfile=/var/log/supervisor/%(program_name)s.log
脚本将写入日志,然后由管理员读取,在 Web 界面上可见。如果您尝试在没有主管的情况下启动脚本,它可能会产生一个问题,即权限被拒绝。