Ivan Borshchov
Ivan Borshchov

Reputation: 3545

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?

Upvotes: 1

Views: 1391

Answers (1)

Jeroen Jacobs
Jeroen Jacobs

Reputation: 1525

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.

Upvotes: 2

Related Questions