gpt4 book ai didi

Python 日志记录 : disable output to stdout

转载 作者:太空宇宙 更新时间:2023-11-04 08:02:39 25 4
gpt4 key购买 nike

我试图让程序只使用 SysLogHandler 实例进行日志记录,而不使用其他处理程序。我希望它不会记录到任何文件或标准输出。

    self.logger = logging.getLogger(self.name)

syslog_handler = logging.handlers.SysLogHandler(
socktype=socket.AF_UNIX,
address='/dev/log',
facility=logging.handlers.SysLogHandler.LOG_LOCAL4,
)

# Check if there is a handler attached
if len(self.logger.handlers) > 0:

# If there is a handler attached
# ensure it is a SysLogHandler instance

handler = self.logger.handlers[0]
if not (handler.__class__ == logging.handlers.SysLogHandler):
# If is was something else but a SysLogHandler instance,
# remove it and attach the syslog_handler

self.logger.handlers = []
self.logger.addHandler(syslog_handler)
else:
# If no handlers attached,
# attach syslog_handler

self.logger.handlers = []
self.logger.addHandler(syslog_handler)

但使用此设置,当使用 python srcipt.py

运行时,它会继续将行输出到标准输出

最佳答案

你有不同的方式来做到这一点:

-将 logger.propagate 设置为 false。

并将其提供给您的记录器。

logging.StreamHandler(stream=None)

关于Python 日志记录 : disable output to stdout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37460714/

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