import logging
import daemon
Channe = "/test/test/log/"
channl_name = "test"
logging.basicConfig(filename=Channe + test_name + ".log",level=logging.INFO,format='%(asctime)s:%(levelname)s:%(message)s')
logging.info("test")
with daemon.DaemonContext():
logging.info("test")
这样写时, logging.info("开始开服服")这一句始终写不进日志文件中。
这是运行daemon时把日志IO都关闭了。
这样写就ok了。
import logging
import daemon
Channe = "/python/YueNan/log/"
channl_name = "yuenan"
logging.basicConfig(filename=Channe + test_name + ".log",level=logging.INFO,format='%(asctime)s:%(levelname)s:%(message)s')
logger = logging.getLogger()
#得到io地址:[<_io.TextIOWrapper name='/python/YueNan/log/yuenan.log' mode='a' encoding='UTF-8'>]
logger_io= [handler.stream for handler in logger.handlers]
logging.info("test")
with daemon.DaemonContext(files_preserve = logger_io):
logging.info("test")
本文介绍了一种在Python daemon环境中正确记录日志的方法。通过保存logger的IO句柄并在daemon上下文中使用files_preserve参数,确保日志可以被正确写入文件。此技巧解决了daemon环境下日志无法写入的问题。
2177

被折叠的 条评论
为什么被折叠?



