简体   繁体   中英

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

I have 2 loggers

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

logging.config.dictConfig(my_logging_configs)

And if I don't call it the second time The len(logger.handlers) remains 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.

ideas?

You should configure all of your loggers, handlers, filters etc. in a single dictConfig() call. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM