python记录没有基本概况就不会输出较低的水平
以下代码未输出测试
喜欢预期的。
logger = logging.getLogger('foo')
logger.setLevel(logging.DEBUG)
logger.debug('test')
解决方案是将loggging.basicconfig()
添加到文件顶部。为什么这是特别的?据我了解,basic Config
仅设置 root 记录器的流处理程序和格式化器。但是我正在实例化新的记录器,logging.getlogger('foo')
,所以我不确定根记录器如何影响这个新的记录器。
相反,我可以理解下面的代码如何不需要basic Config
,因为您正在为新记录器设置处理程序和格式化器,logging.getlogger('my_logger')
logger = logging.getLogger('my_logger')
f_handler = logging.StreamHandler()
logger.addHandler(f_handler)
logger.setLevel(logging.DEBUG)
logger.info('test')
The below code doesn't output test
like intended.
logger = logging.getLogger('foo')
logger.setLevel(logging.DEBUG)
logger.debug('test')
The solution is to add a logging.basicConfig()
to the top of the file. Why is this specifically? To my understanding basicConfig
only sets up a stream handler and formatter for the root logger. But I am instantiating a new logger, logging.getLogger('foo')
, so I'm not sure how the root logger affects this new logger.
Conversely, I can understand how the below code doesn't need a basicConfig
since you're setting up a handler and formatter for the new logger, logging.getLogger('my_logger')
logger = logging.getLogger('my_logger')
f_handler = logging.StreamHandler()
logger.addHandler(f_handler)
logger.setLevel(logging.DEBUG)
logger.info('test')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
输出是由处理程序完成的,您必须手动添加一个处理程序(如在第二个片段中),或者使用
basic Conconfig()
才能将其添加到根记录器中。如果您没有配置处理程序,则内部“最后度假胜地处理程序”的使用,其级别设置为警告
(因此它不会输出debug
或info
消息)。Output is done by handlers, and you either have to add a handler manually (as in your second snippet) or use
basicConfig()
to add one to the root logger. If no handler is configured by you, an internal "handler of last resort" is used, whose level is set toWARNING
(so it won't outputDEBUG
orINFO
messages).