我试图在 python 代码执行的运行时删除 StreamHandler。
if (False == consoleOutput):
lhStdout = log.handlers[0] # stdout is the only handler initially
log.removeHandler(lhStdout)
这工作正常。但我不喜欢我们假设 stdout 是处理程序数组中的第一个处理程序。有没有办法查询处理程序类以查找它是哪种类型?像这样
for handler in log.handlers
if (handler.type == StreamHandler())
<...>
最佳答案
你要找的是拼写的:if isinstance(handler, StreamHandler):
- 但我真的很想知道为什么你想做这样一个而不是使用明智的解决方案(即根本不为您的记录器配置 StreamHandler ...)。
关于 python : Logging : Removing StreamHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31239322/