Search code examples
pythondjangosentryraven

Why does Sentry's recommended Django logging configuration silence logging from django.db.backends?


The (now deprecated) Sentry Django integration documentation recommends the following LOGGING configuration (https://docs.sentry.io/clients/python/integrations/django/):

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'root': {
        'level': 'WARNING',
        'handlers': ['sentry'],
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s  %(asctime)s  %(module)s '
                      '%(process)d  %(thread)d  %(message)s'
        },
    },
    'handlers': {
        'sentry': {
            'level': 'ERROR', # To capture more than ERROR, change to WARNING, INFO, etc.
            'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
            'tags': {'custom-tag': 'x'},
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        }
    },
    'loggers': {
        'django.db.backends': {
            'level': 'ERROR',
            'handlers': ['console'],
            'propagate': False,
        },
        'raven': {
            'level': 'DEBUG',
            'handlers': ['console'],
            'propagate': False,
        },
        'sentry.errors': {
            'level': 'DEBUG',
            'handlers': ['console'],
            'propagate': False,
        },
    },
}

I am particularly interested in this logger definition:

    'django.db.backends': {
        'level': 'ERROR',
        'handlers': ['console'],
        'propagate': False,
    },

To me, this reads that log entries originating from django.db.backends (and descendants) are sent to the console only, and do not propagate up to the root logger (which is configured to send log entries >= WARNING to Sentry.

The Sentry documentation does not seem to even acknowledge the existence of this logger. This configuration is presented as-is as a means of getting Django to log to Sentry.

  1. Is my reading of this correct?
  2. Can you think of any justification for this? It seems like you'd want to get django.db.backends.* >= WARNING log entries sent to Sentry.

Solution

  • django.db.backends contains logs for every SQL query, and that's the only thing Django uses it for. To my knowledge there are no warnings or errors ever logged on this namespace.

    We want to ignore logging breadcrumbs from django.db.backends because we already have a custom hook for capturing SQL queries in Django, such that we can show them differently from other breadcrumbs in the UI.

    Raven offloads this logic to the user, the new SDK sentry_sdk ignores the logger internally. You can find the equivalent here: https://github.com/getsentry/sentry-python/blob/fde5576e489dd7926fce7bae7be8399021cdde14/sentry_sdk/integrations/django/init.py#L365