How to use sentry/raven in django views

grillermo picture grillermo · May 30, 2012 · Viewed 7.7k times · Source

I managed to install sentry successfully and I can see the sentry interface webservice at localhost and doing a

raven test http://jsifslkdjfklsdfjklsdjfklMYCODE 

works, the tests shows up in the interface.

The problem is I can't find any examples or documentation on what exactly should I put on my views and my settings.

I know I have to add to my INSTALLED_APPS

'sentry', 'raven.contrib.django',

And I also added

SENTRY_DNS = 'http://jsifslkdjfklsdfjklsdjfklMYCODE'

This next two lines appear in the docs but it doesnt say where do they go

from raven.contrib.django.models import client
client.captureException()

I tried in settings.py but still I can't get my views to log anything. I also added this

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',
            'class': 'raven.contrib.django.handlers.SentryHandler',
        },
        '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,
        },
    },
}

And in my views I added this:

import logging
logger = logging.getLogger()

def home(request,template_name): 
    logger.error('There was some crazy error lol', exc_info=True, extra={'request': request, })
    return render_to_response(template_name,context, context_instance=RequestContext(request))

I have no other code related to logging apart from what you see here, What am I missing?

Answer

RickyA picture RickyA · Jun 21, 2012

Your 'raven' logger is not actually using the sentry handler, but only writing to 'console'. Had the same problem. The documentation for raven/sentry lacks a good writer.

change your raven logger to:

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

and make sure you use it as logger:

logger = logging.getLogger('raven')