pythondjangologgingdjango-logging

Django log info as well as error logs on production


I am trying to log INFO as well as ERROR log on production. But I am not sure how to mention both log levels for the same project.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'root': {
        'level': 'WARNING',
        'handlers': ['console', ],
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s '
                      '%(process)d %(thread)d %(message)s'
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        },
        'sns': {
            'level': 'ERROR',
            'class': 'project.abc.snshandler.SNSHandler',
            'formatter': 'verbose'

        }
    },
    'loggers': {
        'django.db.backends': {
            'level': 'ERROR',
            'handlers': ['console', ],
            'propagate': False,
        },
        'django.security.DisallowedHost': {
            'level': 'ERROR',
            'handlers': ['console', ],
            'propagate': False,
        },
        'project': {
            'level': 'ERROR',
            'handlers': ['console', 'sns'],
            'propagate': False,
        },

    },
}

At project level i want to log INFO logs as well.


Solution

  • The logging level you specify in your configuration is the minimum level will be logged. Just to recap, here the predefined levels in order of importance (from lowest to maximum):

    So, if you put INFO in your configuration you will see all logs marked as INFO, WARNING, ERROR, and CRITICAL.

    If you really want to see only INFO and ERROR you can try to write some custom code, but I discourage it as it smells as a bad design.

    References:

    Edit

    Here how your configuration would look like:

    LOGGING = {
        'version': 1,
        'disable_existing_loggers': True,
        'root': {
            'level': 'WARNING',
            'handlers': ['console', ],
        },
        'formatters': {
            'verbose': {
                'format': '%(levelname)s %(asctime)s %(module)s '
                          '%(process)d %(thread)d %(message)s'
            },
        },
        'handlers': {
            'console': {
                'level': 'INFO',
                'class': 'logging.StreamHandler',
                'formatter': 'verbose'
            },
            'sns': {
                'level': 'ERROR',
                'class': 'project.abc.snshandler.SNSHandler',
                'formatter': 'verbose'
    
            }
        },
        'loggers': {
            'django.db.backends': {
                'level': 'INFO',
                'handlers': ['console', ],
                'propagate': False,
            },
            'django.security.DisallowedHost': {
                'level': 'INFO',
                'handlers': ['console', ],
                'propagate': False,
            },
            'project': {
                'level': 'INFO',
                'handlers': ['console', 'sns'],
                'propagate': False,
            },
    
        },
    }