gpt4 book ai didi

Python 守护进程 : keep logging

转载 作者:行者123 更新时间:2023-11-28 22:57:03 24 4
gpt4 key购买 nike

我有一个脚本可以将一些数据记录到磁盘:

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 中,但这也没有帮助。

最佳答案

如何在守护进程中配置记录器?这对我有用:

#!/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()

关于Python 守护进程 : keep logging,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14674733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com