简体   繁体   English

如果我调用logging.config.dictConfig,记录器将在初始化后发生更改

[英]logger is changing after init if I call logging.config.dictConfig

I have 2 loggers 我有2个记录器

logger = logger_setup.get_logger_setup(env, logger_name = 'root',  report_to_logging_service=report_to_logging_service)
print len(logger.handlers) # =3
auditor = logger_setup.get_logger_setup(env, logger_name = 'audit')
print len(logger.handlers) # =2

inside get_logger_setup there is get_logger_setup里面有

logging.config.dictConfig(my_logging_configs)

And if I don't call it the second time The len(logger.handlers) remains 3. 如果我第二次不打电话, len(logger.handlers)仍然是3。

I have to call it every time I init a new logger in case of different settings. 如果设置不同,每次启动新记录器时都必须调用它。

Tried to deep copy copy.deepcopy(logger) to create a separate object that will not be coupled to logging.config.dictConfig but it error since it's a complex object. 尝试对深层副本copy.deepcopy(logger)创建一个单独的对象,该对象不会与logging.config.dictConfig耦合,但由于它是一个复杂的对象而会出错。

ideas? 想法?

You should configure all of your loggers, handlers, filters etc. in a single dictConfig() call. 您应该在单个 dictConfig()调用中配置所有记录器,处理程序,过滤器等。 This page has numerous example configurations that you can use as a starting point. 该页面包含许多示例配置,您可以以此为起点。 One such (more complex than you might need, but the linked page has simpler examples) is given below: 下面给出了一种这样的示例(比您可能需要的复杂,但链接的页面具有更简单的示例):

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'filters': {
        'special': {
            '()': 'project.logging.SpecialFilter',
            'foo': 'bar',
        },
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'handlers': {
        'console': {
            'level': 'INFO',
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            'filters': ['special']
        }
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
        },
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': False,
        },
        'myproject.custom': {
            'handlers': ['console', 'mail_admins'],
            'level': 'INFO',
            'filters': ['special']
        }
    }
}

The call to dictConfig() is made in application (ie your ) code - and should not be in the code of any libraries you use. dictConfig()的调用是在应用程序(即您的 )代码中进行的-不应在您使用的任何库的代码中进行。 If any of them are doing logging configuration beyond what's recommended for libraries in the Python documentation, then issues should be raised with their maintainers to rectify this. 如果他们中的任何人进行的日志记录配置超出了Python文档中对库的建议,那么其维护者应提出问题以纠正此问题。

暂无
暂无

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

相关问题 logging.config.dictConfig似乎不起作用 - logging.config.dictConfig does not seem to work logging.config.dictConfig 的完整示例在哪里? - Where is a complete example of logging.config.dictConfig? Python的logging.config.dictConfig()是否应用记录器的配置设置? - Does Python's logging.config.dictConfig() apply the logger's configuration settings? 配置日志记录在python中使用logging.config.dictConfig时出错 - Error when config logging use logging.config.dictConfig in python Python 使用 logging.config.dictConfig 以 {} 格式记录 - Python logging with {} format using logging.config.dictConfig 带有 logging.config.dictConfig 的 JSON 日志格式器 - JSON-log-formatter with logging.config.dictConfig 如何通过 logging.config.dictConfig - FileHandler 指定 namer & rotator - How to specify namer & rotator through logging.config.dictConfig - FileHandler 哪个模块应该包含logging.config.dictConfig(my_dictionary)? my_dictionary怎么样? - Which module should contain logging.config.dictConfig(my_dictionary)? What about my_dictionary? 在日志文件名中包含日期并创建特定大小的日志文件,同时使用 `logging.config.dictConfig()` 在 python 中保留最后 3 个日志文件 - Including date in the log file name & creating log files of certain size while keeping last 3 log files in python with `logging.config.dictConfig()` Python日志dictConfig中的常见日志记录器设置 - Common logger settings in Python logging dictConfig
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM