4

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?")

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.