0
votes

I am trying to enable email alerts when HTTP 500 errors happens. Everything works fine, except that a Authentication credentials were not provided (HTTP 401) error is logged at ERROR level, which is way too verbose for email alerts.

Here is the email content (Django settings omitted here):

Report at /

[35mb'{"detail":"Authentication credentials were not provided."}' [0m

Request Method: GET

Request URL: https://myapi.org/dev/

Django Version: 2.0.2

Python Executable: /var/lang/bin/python3.6

Python Version: 3.6.1

Also, I am using the django-request-logging module to print requests' content.

Here is my logging config:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            'email_backend': 'django.core.mail.backends.smtp.EmailBackend',
            'include_html': True,
            'filters': [],
            'formatter': 'simple'
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['console', 'mail_admins'],
            'level': 'DEBUG',
            'propagate': False,
        },
    },
}

Any idea how I could filter out HTTP 401 from the mail_admins handler?

1

1 Answers

3
votes

From the documentation for django-request-logger:

If HTTP status code is between 400 - 599, URIs are logged at ERROR level, otherwise they are logged at INFO level.

See REQUEST_LOGGING_HTTP_4XX_LOG_LEVEL setting to override this.

And the documentation for that setting explains how to change the log level:

By default, HTTP status codes between 400 - 499 are logged at ERROR level. You can set REQUEST_LOGGING_HTTP_4XX_LOG_LEVEL = logging.WARNING (etc) to override this.