[英]Python logging: disable output to stdout
我正在嘗試使程序僅使用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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.