How to log INFO logs in settings.py

242 Views Asked by At

I'm trying to log some info messages specifically in django settings.py. I'm following the first example in the django documentation that shows a simple configuration. The only thing I changed was having the log show INFO level messages. When I run python manage.py runserver, the log statement is never shown. The logs are only shown for WARNING level or above (more critical).

Here is my configuration that is located in settings.py

import logging
...
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    '': {
        'handlers': ['console'],
        'level': 'INFO',
        'propagate': True
    },
}
...
def do_something():
    logger.info('Doing Something')
do_something()

Note I'm trying to log statements only in my settings.py file as of right now.

1

There are 1 best solutions below

1
Nealium On

Try Placing a level key inside the handler

    'handlers': {
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
        },
    },

I just tested this on my Local Dev, My Logger looks like this [Chopped]: (Hopefully this helps!)

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'handlers': {
        'console': {
            'level': 'INFO',
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': 'INFO',
        },
    }
}

def do():
    import logging
    logger = logging.getLogger('django')
    logger.info('TEST')