Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Use logging inside settings.py

I successfully set up Django logging, but I find myself in need to log some information inside settings.py. Is that possible?

Currently, the settings only come into effect after the entire file is parsed, which I believe is expected. Is there a way to force the set up of loggers right after specifying them? Otherwise, are there any alternatives that people recommend?

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'console': {
            'format': '%(levelname)s: %(message)s'
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'console'
        }
    },
    'loggers': {
        'main': {
            'handlers': ['console'],
            'level': 'INFO',
            'propagate': False,
        },
    },
}

# The below does not work because the setup declared above is not yet in effect

logger = logging.getLogger('main')
logger.info("Works?")
like image 989
Andrei Cioara Avatar asked Oct 23 '25 14:10

Andrei Cioara


1 Answers

Answering my own question, but will accept other answers that might be more complete

Adding the following, will load the LOGGING as required. Not sure what are the side effects.

import logging.config
logging.config.dictConfig(LOGGING)
like image 107
Andrei Cioara Avatar answered Oct 26 '25 05:10

Andrei Cioara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!