[Django]-How to send Exceptions to sentry for a django project

4👍

From the sentry docs

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,
    },
},

}

Link to doc: https://docs.sentry.io/clients/python/integrations/django/

EDIT: From the comment

try:
    do something
except Exception:
    from raven.contrib.django.raven_compat.models import client
    client.captureException()

if you don’t want to do this manually and want sentry logging to occur anytime an exception block is triggered anywhere in your project, use the logging based solution above.

👤Mehran

3👍

Just checking in to say that the other answers are totally outdated.

The raven library has been deprecated in favor of sentry-sdk and setting this up does not involve mangling the logging configuration anymore, since a default logging integration will already spy on any logger call.

Just do

import sentry_sdk.integrations.django

sentry_sdk.init(
    integrations=[sentry_sdk.integrations.django.DjangoIntegration()],
)

in your settings.py and you are good to go (remember to export a SENTRY_DSN variable and whatnot).

See https://docs.sentry.io/platforms/python/guides/django/#configure

👤N1ngu

1👍

Updated Sentry documents mention that with Django integration, you don’t need to explicitly define LOGGER in settings.py:

import logging

logging.error("Test error event", extra=dict(bar=43))

0👍

As @Mehran said after configuration of your LOGGING in settings.py, you will be able to use logging. Let’s assume that you have example in loggers in configuration like;

'loggers': {        
    'example': {
        'handlers': ['console', 'sentry'],
        'level': 'DEBUG',
        'propagate': False
    }
}

Then;

import logging

logger = logging.getLogger("example")

def test():
    try:
       # some staff
    except Exception as error:
       logger.error("Custom Error Message %s" %error)

logger also have warn, info etc.

0👍

It is also possible to post directly to your Sentry DSN without using any of the Sentry libraries, but using pure requests. This can be useful if you’re running a version of Python or Django that isn’t compatible with Sentry’s latest DSN.

I’ve posted a code for this -including an example test- here

Leave a comment