简体   繁体   中英

How to log INFO logs in settings.py

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.

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM