我有一个将一些数据记录到磁盘的脚本:
logging.basicConfig(filename='davis-debug.log',level=logging.DEBUG)
logging.basicConfig(filename='davis-error.log',level=logging.ERROR)
logging.basicConfig(filename='davis-error.log',level=logging.WARNING)
logging.basicConfig(filename='davis-error.log',level=logging.CRITICAL)
当我像这样使用python-daemon时,日志记录停止。
try:
with daemon.DaemonContext():
station = VantageProStation()
station.run()
except KeyboardInterrupt:
logging.critical('Stopping user aborted with CTRL+C')
pass
我尝试过file_preserve,但logging.basicConfig不会返回流
另外我不能使用files_preserve传递几个流。?
我希望我的日志记录继续,我试图将日志定义放在我的班级 init 中,这也没有帮助。
答案 0 :(得分:4)
如何在守护程序中配置记录器?这对我有用:
#!/usr/bin/env python
import daemon
import logging
import logging.handlers
from time import sleep
from datetime import datetime
def time_logging_daemon():
logger = logging.getLogger('time_logging_daemon')
logger.addHandler(logging.handlers.SysLogHandler(address='/dev/log'))
logger.setLevel(logging.INFO)
while True:
logger.info(datetime.now())
sleep(1)
with daemon.DaemonContext():
time_logging_daemon()
答案 1 :(得分:0)
守护程序上下文将所有文件处理程序关闭为#34;表现良好的守护程序"除了files_preserve
中指定的那些。你是在正确的方向。
有一种方法可以从logging
获取文件处理程序,但我建议你明确地创建文件记录器。
#!/usr/bin/env python
import daemon
import logging
import logging.handlers
from time import sleep
from datetime import datetime
logging.basicConfig()
file_logger = logging.FileHandler("/tmp/aaa.log", "w")
logger = logging.getLogger()
logger.addHandler(file_logger)
logger.setLevel(logging.INFO)
with daemon.DaemonContext(files_preserve=[file_logger.stream.fileno()]):
while True:
logger.info(datetime.now())
sleep(1)