19
votes

I tried to change the debug level to DEBUG in Django because I want to add some debug messages to my code. It seems to have no effect.

My logging configuration:

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

Code:

import logging ; logger = logging.getLogger(__name__)
logger.debug("THIS MESSAGE IS NOT SHOWN IN THE LOGS")
logger.warn("THIS ONE IS")

Output on the console:

WARNING:core.handlers:THIS ONE IS

I also have tried setting DEBUG = False and DEBUG = True in my settings file. Any ideas?

Edit: If I set the log level on the logger directly, it works:

import logging ; logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.debug("THIS MESSAGE IS NOT SHOWN IN THE LOGS")

Output:

DEBUG:core.handlers:THIS MESSAGE IS NOT SHOWN IN THE LOGS
WARNING:core.handlers:THIS ONE IS

But: It seems the config file is totally ignored. It prints both statements always even if I set both entries in the config back to ERROR. Is this the correct behaviour or am I still missing something?

2
Quick check: logger.warn("Log level is set to {0}".format(logging.getLevelName(logger.level))) - Christian Ternus
WARNING:core.handlers:Log level is set to NOTSET. So it is the default level which is WARNING, right? Why doesn't Django change it accordingly to my settings? - kev
blink Huh. I got nothing. It looks to me like you're doing everything right. - Christian Ternus
I updated my answer in response to your comment. - Vinay Sajip

2 Answers

13
votes

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.

5
votes

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'],
        },
    },
})