我正在使用Python的日志记录模块,并且想在一段时间内禁用控制台日志记录,但是它不起作用。#!/usr/bin/pythonimport logginglogger = logging.getLogger() # this gets the root logger# ... here I add my own handlers #logger.removeHandler(sys.stdout)#logger.removeHandler(sys.stderr)print logger.handlers # this will print [<logging.StreamHandler instance at ...>]# but I may have other handlers there that I want to keeplogger.debug("bla bla")上面的代码显示了bla blaon stdout,我不知道如何安全地禁用控制台处理程序。如何确定我暂时删除了控制台StreamHandler而不是另一个?
3 回答

绝地无双
TA贡献1946条经验 获得超4个赞
我找到了一个解决方案:
logger = logging.getLogger('my-logger')
logger.propagate = False
# now if you use logger it will not log to console.
这将防止日志记录发送到包括控制台日志记录的上层记录器。

鸿蒙传说
TA贡献1865条经验 获得超7个赞
我用:
logger = logging.getLogger()
logger.disabled = True
... whatever you want ...
logger.disabled = False
添加回答
举报
0/150
提交
取消