Vineet Patel
Vineet Patel

Reputation: 499

Django info and debug logs ignored, despite configuration

So I am trying to set up logging in a Django program. I set up the logging configurations in settings.py:

    DEBUG = True
    LOGGING_CONFIG = None
    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'custom': {
                'format': '%(asctime)s %(levelname)-8s %(name)-15s %(message)s'
            }
        },
        'handlers': {
            'console': {
                'level': 'NOTSET',
                'class': 'logging.StreamHandler',
                'formatter': 'custom'
            }
        },
        'loggers': {
            '': {
                'handlers': ['console'],
            }
        }
    }
    import logging.config

    logging.config.dictConfig(LOGGING)

And then I do the following:

import logging
logger = logging.getLogger(__name__)

logger.info("INFO")
logger.debug("DEBUG")
logger.warn("WARN")
logger.critical("CRITICAL")
logger.error("ERROR")

But I only get the following output:

2019-05-21 14:08:31,877 WARNING  dashboards.charts WARN
2019-05-21 14:08:31,877 CRITICAL dashboards.charts CRITICAL
2019-05-21 14:08:31,877 ERROR    dashboards.charts ERROR

I tried changing the level to DEBUG or info, but that didn't change anything. The formatter works correctly, so I don't know why the level won't work.

Upvotes: 1

Views: 175

Answers (1)

Danilo Akamine
Danilo Akamine

Reputation: 765

Try putting level inside loggers instead of handlers. For instance:

'handlers': {
    'console': {
        'class': 'logging.StreamHandler',
        'formatter': 'custom'
    }
},
'loggers': {
    '': {
        'handlers': ['console'],
        'level': 'DEBUG'
    }
}

Upvotes: 2

Related Questions