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

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

问题描述

看起来,如果您在运行 logging.basicConfig 之前调用 logging.info() BEFORE,则 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天全站免登陆