相关疑难解决方法(0)

使用Python Logging记录消息两次

我正在使用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)
Run Code Online (Sandbox Code Playgroud)

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

if __name__ == '__main__':
    tom = Boy()
    tom.configure_logging(LOG_FILENAME)
    tom.buy_ham()
Run Code Online (Sandbox Code Playgroud)

然后说,buy_ham模块,我打电话给:

self.logger.info('Successfully able to write …
Run Code Online (Sandbox Code Playgroud)

python logging

78
推荐指数
6
解决办法
4万
查看次数

标签 统计

logging ×1

python ×1