python记录没有基本概况就不会输出较低的水平

发布于 2025-02-10 14:49:48 字数 745 浏览 2 评论 0原文

以下代码未输出测试喜欢预期的。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月染过的梦 2025-02-17 14:49:48

输出是由处理程序完成的,您必须手动添加一个处理程序(如在第二个片段中),或者使用basic Conconfig()才能将其添加到根记录器中。如果您没有配置处理程序,则内部“最后度假胜地处理程序”的使用,其级别设置为警告(因此它不会输出debuginfo消息)。

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 to WARNING (so it won't output DEBUG or INFO messages).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文