使用 Python 日志记录出现两次的日志消息 [英] log messages appearing twice with Python Logging

查看:37
本文介绍了使用 Python 日志记录出现两次的日志消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 日志记录,但出于某种原因,我的所有消息都出现了两次.

I'm using Python logging, and for some reason, all of my messages are appearing twice.

我有一个配置日志的模块:

I have a module to configure logging:

# BUG: It's outputting logging messages twice - not sure why - it's not the propagate setting.
def configure_logging(self, logging_file):
    self.logger = logging.getLogger("my_logger")
    self.logger.setLevel(logging.DEBUG)
    self.logger.propagate = 0
    # Format for our loglines
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    # Setup console logging
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    ch.setFormatter(formatter)
    self.logger.addHandler(ch)
    # Setup file logging as well
    fh = logging.FileHandler(LOG_FILENAME)
    fh.setLevel(logging.DEBUG)
    fh.setFormatter(formatter)
    self.logger.addHandler(fh)

稍后,我调用此方法来配置日志记录:

Later on, I call this method to configure logging:

if __name__ == '__main__':
    tom = Boy()
    tom.configure_logging(LOG_FILENAME)
    tom.buy_ham()

然后说,buy_ham 模块,我会调用:

And then within say, the buy_ham module, I'd call:

self.logger.info('Successfully able to write to %s' % path)

出于某种原因,所有消息都出现了两次.我注释掉了一个流处理程序,仍然是同样的事情.有点奇怪,不知道为什么会这样......大声笑.假设我错过了一些明显的东西.

And for some reason, all the messages are appearing twice. I commented out one of the stream handlers, still the same thing. Bit of a weird one, not sure why this is happening...lol. Assuming I've missed something obvious.

干杯,维克多

推荐答案

您正在调用 configure_logging 两次(可能在 Boy__init__ 方法中>) :getLogger 将返回相同的对象,但 addHandler 不会检查是否已将类似的处理程序添加到记录器中.

You are calling configure_logging twice (maybe in the __init__ method of Boy) : getLogger will return the same object, but addHandler does not check if a similar handler has already been added to the logger.

尝试跟踪对该方法的调用并消除其中之一.或者在Boy__init__方法中设置一个初始化为False的标志logging_initialized并改变configure_logging 如果 logging_initializedTrue,则不执行任何操作,并在初始化记录器后将其设置为 True.

Try tracing calls to that method and eliminating one of these. Or set up a flag logging_initialized initialized to False in the __init__ method of Boy and change configure_logging to do nothing if logging_initialized is True, and to set it to True after you've initialized the logger.

如果您的程序创建了多个 Boy 实例,则您必须使用全局 configure_logging 函数添加处理程序和 Boy.configure_logging 方法只初始化 self.logger 属性.

If your program creates several Boy instances, you'll have to change the way you do things with a global configure_logging function adding the handlers, and the Boy.configure_logging method only initializing the self.logger attribute.

解决此问题的另一种方法是检查记录器的处理程序属性:

Another way of solving this is by checking the handlers attribute of your logger:

logger = logging.getLogger('my_logger')
if not logger.handlers:
    # create the handlers and call logger.addHandler(logging_handler)

这篇关于使用 Python 日志记录出现两次的日志消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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