The Python Oracle

Django: how to set log level to INFO or DEBUG

This video explains
Django: how to set log level to INFO or DEBUG

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Orient Looping

--

Chapters
00:00 Question
01:38 Accepted answer (Score 16)
02:32 Answer 2 (Score 5)
03:15 Thank you

--

Full question
https://stackoverflow.com/questions/1972...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #django #logging #configurationfiles

#avk47



ACCEPTED ANSWER

Score 17


You need to add e.g.

'core.handlers': {
    'level': 'DEBUG',
    'handlers': ['console']
}

in parallel with the django.request entry, or

'root': {
    'level': 'DEBUG',
    'handlers': ['console']
}

in parallel with the 'loggers' entry. This will ensure that the level is set on the logger you are actually using, rather than just the django.request logger.

Update: To show messages for all your modules, just add entries alongside django.request to include your top level modules, e.g. api, handlers, core or whatever. Since you haven't said exactly what your package/module hierarchy is, I can't be more specific.




ANSWER 2

Score 6


I fixed it by changing

LOGGING = {
    ...
}

to:

logging.config.dictConfig({
    ...
})

For example to log all messages to the console:

import logging.config
LOGGING_CONFIG = None
logging.config.dictConfig({
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'console': {
            # exact format is not important, this is the minimum information
            'format': '%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'console',
        },
    },
    'loggers': {
    # root logger
        '': {
            'level': 'DEBUG',
            'handlers': ['console'],
        },
    },
})



ANSWER 3

Score 2


I guess, this is a minimum configuration with settings.py to change log level with root logger.

LOGGING = {
    "version": 1,
    'disable_existing_loggers': False,
    "root": {
        "level": "INFO"
    }
}