简体   繁体   中英

Common logger settings in Python logging dictConfig

I use logging.config.dictConfig in my python application:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,

    'formatters': {
        'verbose': {
            """ MY_FORMATTER_SETTINGS """
        },
    },

    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/var/log/mylogger.log',
            'formatter': 'verbose'
        },
    },

    'loggers': {  
        'logger1': {
            'handlers': ['file'],
            'level': 'DEBUG',
        },
        'logger2': {
            'handlers': ['file'],
            'level': 'DEBUG',
        },
       'logger3': {
            'handlers': ['file'],
            'level': 'DEBUG',
        },  
        'logger4': {
            'handlers': ['file'],
            'level': 'DEBUG',
        },  
    }
}

logging.config.dictConfig(LOGGING)

Then I create loggers in some modules via logger = logging.getLogger("logger1") etc, and use them, for example logger.info("Some info") , without any additional configuration. I like it, but only thing I want to optimize is loggers setting, by providing some common(or default?) value for all loggers. Is it possible?

What happens if you define a root logger in your "loggers" section?

'loggers': {
        #root logger  
        '': {
            'handlers': ['file'],
            'level': 'DEBUG',
        },
        # other loggers
        ...
}

The root logger contains the default log settings, and specifiy additional loggers for the special cases.

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