运行logging.basicConfig之前的Python日志记录?

发布于 2021-01-29 17:06:02

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

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

关注者
0
被浏览
49
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

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

    # 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)
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看