【问题标题】:Having difficulties understanding Python's logger propagation难以理解 Python 的记录器传播
【发布时间】:2021-03-23 14:39:00
【问题描述】:

在定义我的记录器配置的logging.json 文件中,我定义了一个记录器script_logger,并设置了一个处理程序console,以将每个记录消息输出到标准输出。它有"propagate": "no",因为我不希望将事件传播到根记录器,但是当我创建记录消息时,它无论如何都会在标准输出上显示两次。

没有两次消息的唯一方法是从root 记录器中删除处理程序consolepropagate 属性好像没有效果。

为什么?有人可以帮忙吗?

$ python test_script.py
2015-09-02 14:26:35,436 - script_logger - DEBUG - debug msg
2015-09-02 14:26:35,436 - script_logger - DEBUG - debug msg

test_script.py

import logging

if __name__ == "__main__":

    logger = logging.getLogger("script_logger")
    logger.debug("debug msg")

logging.json

{
    "version": 1,
    "disable_existing_loggers": false,
    "formatters": {
        "simple": {
            "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
        }
    },

    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "level": "DEBUG",
            "formatter": "simple",
            "stream": "ext://sys.stdout"
        }, 

        "info_file_handler": {
            "class": "logging.handlers.RotatingFileHandler",
            "level": "INFO",
            "formatter": "simple",
            "filename": "info.log",
            "maxBytes": 10485760,                                                                      
            "backupCount": 20,
            "encoding": "utf8"
        },

        "error_file_handler": {
            "class": "logging.handlers.RotatingFileHandler",                                           
            "level": "ERROR",                                                                          
            "formatter": "simple",
            "filename": "errors.log",                                                                  
            "maxBytes": 10485760,                                                                      
            "backupCount": 20,
            "encoding": "utf8"                                                                         
        }                                                                                              
    },                                                                                                 

    "loggers": {
        "script_logger": {
            "level": "DEBUG",                                                                          
            "handlers": ["console"],                                                                   
            "propagate": "no"
        }
    },                                                                                                 

    "root": {
        "level": "DEBUG",
        "handlers": ["console", "info_file_handler", "error_file_handler"]                             
    }       
} 

【问题讨论】:

    标签: python logging


    【解决方案1】:

    该消息被记录了两次,因为 console 记录器同时在 rootscript_logger 中 -- 但我想你已经知道了。

    只需将"propagate": "no" 替换为"propagate": False,一切都会如您所愿。

    【讨论】:

    猜你喜欢
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 2018-09-09
    • 2011-12-07
    相关资源
    最近更新 更多