我使用Python日志记录,并且由于某种原因,我的所有消息都出现了两次。
我有一个模块来配置日志:
# 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)
后来,我调用这个方法来配置日志:
if __name__ == '__main__':
tom = Boy()
tom.configure_logging(LOG_FILENAME)
tom.buy_ham()
然后在说,buy_ham模块,我会调用:
self.logger.info('Successfully able to write to %s' % path)
并且由于某种原因,所有的消息出现了两次。我注释掉了一个流处理程序,仍然是同样的事情。位奇怪的,不知道为什么这是发生…哈哈。假设我错过了一些明显的东西。
干杯,
胜利者