繁体   English   中英

为什么Python记录器无法在logging.INFO级别显示信息或调试消息?

[英]Why doesn't Python logger display info or debug message at level logging.INFO?

有人可以向我解释为什么.info()和.debug()调用即使看起来好像也不显示任何内容? 我觉得即使在通过了日志记录模块文档之后,我仍然不了解一些非常基本的内容。

$  python                                    
Python 3.6.5 (default, Apr 25 2018, 14:23:58) 
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logger = logging.getLogger()
>>> logger.setLevel(logging.INFO)
>>> logger.warning('This is a warning. We should see it.')
This is a warning. We should see it.
>>> logger.debug('This is a debug message. We should not see it.')
>>> logger.info('This is an info message. We should... see it, right?')
>>> logger.setLevel(logging.DEBUG)
>>> logger.info('This is an info message. We should... see it, right?')
>>> logger.debug('Weird. So I guess we are not going to see this as well?')
>>> 

它正在记录日志,只是不记录到终端。 如果要查看输出到终端的日志(类似于print ),则需要添加一个处理程序:

logger.addHandler(logging.StreamHandler())

这样做之后,日志记录应该按预期显示在您的终端上。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM