Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: how to set log level to INFO or DEBUG

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?

like image 493
kev Avatar asked Nov 01 '13 04:11

kev


People also ask

How do I enable logs in Django?

By default, the LOGGING setting is merged with Django's default logging configuration using the following scheme. If the disable_existing_loggers key in the LOGGING dictConfig is set to True (which is the dictConfig default if the key is missing) then all loggers from the default configuration will be disabled.

How do I enable debug mode in Django?

The debug mode (DEBUG=True) is turned on by default in the Django framework. It provides a detailed traceback with the local variables to find out the error with the line numbers. The error can be triggered from the view page by setting the value of assert to False in the view file.

What is Django logger propagation?

Logging in a Django ProjectLoggings are propagated to parents whereby logs in a subtree of a hierarchy are also captured by all its parent loggers. The basic Django logging is based on the standard Python logging dictConfig() method that configures logging and creates loggers, handlers, filters, and formatters.

What is the default level in logging module?

By default, the root log level is WARN, so every log with lower level (for example via logging.info("info") ) will be ignored. Another particularity of the root logger is that its default handler will be created the first time a log with a level greater than WARN is logged.


1 Answers

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.

like image 190
Vinay Sajip Avatar answered Sep 19 '22 02:09

Vinay Sajip