我试图让程序仅使用 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)