运行logging.basicConfig之前的Python日志记录?
发布于 2021-01-29 17:06:02
看来,如果 在 运行logging.basicConfig 之前
调用logging.info(),则logging.basicConfig调用不会产生任何效果。实际上,不会发生任何日志记录。
该行为记录在哪里?我不太了解
关注者
0
被浏览
49
1 个回答
-
您可以删除默认处理程序并重新配置日志记录,如下所示:
# 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)