【发布时间】:2015-10-07 08:12:29
【问题描述】:
Python 的 logging.basicConfig 的行为与我预期的不同。在下面的代码中,使用logging.warn 发送的消息即使我调用basicConfig 的级别为ERROR 也会被发送。
import logging
logging.basicConfig(level=logging.ERROR)
root = logging.getLogger()
print("root.level", logging.getLevelName(root.level))
print("Levels for root.handlers: {}".format(
[logging.getLevelName(handler.level) for handler in root.handlers]))
logging.warn("You're seeing this even though you called basicConfig with level=logging.ERROR")
root.warn("You're seeing this too")
root.setLevel(logging.ERROR)
logging.warn("But you won't see this")
产生的输出是
root.level WARNING
Levels for root.handlers: ['NOTSET']
WARNING:root:You're seeing this even though you called basicConfig with level=logging.ERROR
WARNING:root:You're seeing this too
请注意,一旦我自己调用了root.setLevel,对logging.warn 的最终调用确实无法按预期产生输出。
如果logging.basicConfig 的level 参数在这个简单的设置中不起作用,它有什么用?如果我想控制在一个简单的设置中输出什么级别的消息,那么调用root.setLevel 的首选方法是什么?
这是我的版本信息:
# sys.version = 3.4.3 |Continuum Analytics, Inc.| (default, Mar 6 2015, 12:03:53) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
stackoverflow 上已经有几个问题似乎与这个问题相同,但我发现的所有问题实际上都是关于其他事情的:
-
This guy 正在尝试运行
logging.basicConfig在他已经完成一些日志记录之后 -
This question 尝试多次调用
logging.basicConfig(产生类似于 #1 的效果) - This question 涉及使用子记录器
【问题讨论】:
-
使用上面的确切代码,我无法用 python 2.7 或 3.4 复制它。