Igo*_* T. 1 python logging syslog syslog-ng python-3.x
我正在尝试使程序仅使用SysLogHandler实例进行日志记录而不使用其他处理程序.我希望它不会记录到任何文件或stdout.
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)
Run Code Online (Sandbox Code Playgroud)
但是通过这种设置,它继续在运行时将线路吐出到标准输出 python srcipt.py
您有不同的方法:
-set logger.propagate为false.
你可以创建自己的游戏流,像这样:
并将它交给你的记录器.
logging.StreamHandler(stream=None)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4059 次 |
| 最近记录: |