在运行logging.basicConfig之前是Python日志记录吗? [英] Python logging before you run logging.basicConfig?

查看:198
本文介绍了在运行logging.basicConfig之前是Python日志记录吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来,如果您调用logging.info()之前,则运行logging.basicConfig,logging.basicConfig调用没有任何效果.实际上,没有日志记录发生.

It appears that if you invoke logging.info() BEFORE you run logging.basicConfig, the logging.basicConfig call doesn't have any effect. In fact, no logging occurs.

此行为记录在哪里?我不太了解.

Where is this behavior documented? I don't really understand.

推荐答案

您可以删除默认处理程序并重新配置日志记录,如下所示:

You can remove the default handlers and reconfigure logging like this:

# if someone tried to log something before basicConfig is called, Python creates a default handler that
# goes to the console and will ignore further basicConfig calls. Remove the handler if there is one.
root = logging.getLogger()
if root.handlers:
    for handler in root.handlers:
        root.removeHandler(handler)
logging.basicConfig(format='%(asctime)s %(message)s',level=logging.DEBUG)

这篇关于在运行logging.basicConfig之前是Python日志记录吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆