简体   繁体   English

Python 记录器在没有 configure_logging() 的情况下打印两次行

[英]Python logger prints line twice without configure_logging()

When using the python logger, my program starts fine with logging but at some point the log output starts outputting lines twice, looking like this:使用 python 记录器时,我的程序开始时可以很好地记录日志,但在某些时候日志输出开始输出两次行,如下所示:

DEBUG:pluginbrowser:Scanning plugs
DEBUG:pluginbrowser:Doing good stuff
....
INFO:pluginbrowser:=== doing something ===
=== doing something ===

Currently all my python files contain the line目前我所有的python文件都包含该行

LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.DEBUG)

Thus, the doubled message comes from the pluginbrowser.py file.因此,双重消息来自 pluginbrowser.py 文件。 Examining this, I found out that at the beginning of my program, the same files output some log without double-ing the lines.检查这一点,我发现在我的程序开始时,相同的文件输出了一些日志而没有重复行。 I tried to find out at which point exactly it happens but I am somehow stuck here.我试图找出它究竟发生在哪一点,但不知何故我被困在这里。

I also read log messages appearing twice with Python Logging but I am not using configure_logging at all.我还阅读了使用 Python Logging 出现两次的日志消息,但我根本没有使用configure_logging

Looks like you add an additional handler somewhere along the way.看起来您在此过程中的某处添加了一个额外的处理程序。 I would search for addHandler in the code.我会在代码中搜索 addHandler 。 You could also debug and watch logger.root.handlers您还可以调试和观看 logger.root.handlers

This is how I could reproduce your effects:这是我如何重现您的效果:

In [1]: import logging

In [2]: logging.basicConfig(level=logging.DEBUG)

In [3]: logger = logging.getLogger('pluginbrowser')

In [4]: logger.info('=== doing something ===')
INFO:pluginbrowser:=== doing something ===

In [5]: logging.root.addHandler(logging.StreamHandler())

In [6]: logger.info('=== doing something ===')
INFO:pluginbrowser:=== doing something ===
=== doing something ===

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM