我有一个示例记录器,我认为它没有处理程序,但它输出日志消息。这是代码:
import logging
ll = logging.getLogger('ll')
print("Has handlers:", ll.hasHandlers())
print("Handlers:", ll.handlers)
ll.propagate = False
ll.warning("Logging with no Handler!")
结果显示记录器没有处理程序,有一个空的处理程序列表,但它打印输出:
% python example.py
Has handlers: False
Handlers: []
Logging with no Handler!
%
为什么这样做?我需要添加一个NullHandler()
来停止输出吗?
这是因为对于Python来说>=3.2,有一个最后手段处理程序,它将严重性为WARNING
及以上的事件输出到sys.stderr
。