[英]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.