Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use sentry/raven in django views

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?

like image 776
grillermo Avatar asked May 30 '12 19:05

grillermo


People also ask

What is Raven Django?

Raven is a Python client for Sentry. It provides full out-of-the-box support for many of the popular frameworks, including Django, Flask, and Pylons. Raven also includes drop-in support for any WSGI-compatible web application.

What is Raven sentry?

Raven. js is the official browser JavaScript client for Sentry. It automatically reports uncaught JavaScript exceptions triggered from a browser environment, and provides a rich API for reporting your own errors.


2 Answers

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')
like image 143
RickyA Avatar answered Sep 27 '22 20:09

RickyA


I had to use this monstruosity on my settings.py:

import logging
# from raven.contrib.django.handlers import SentryHandler 
from raven.handlers.logging import SentryHandler

logging.getLogger().setLevel(logging.INFO)
logger = logging.getLogger()# ensure we havent already registered the handler
handler = SentryHandler('http://13d06dad246d4fe6a180ef9b15151a13:[email protected]/1')
logger.addHandler(handler)
# Add StreamHandler to sentry's default so you can catch missed exceptions
logger = logging.getLogger('sentry.errors')
logger.propagate = False
logger.addHandler(logging.StreamHandler())


from raven.conf import setup_logging
setup_logging(handler)

And in my views i can use a simple

import logging
logger = logging.getLogger(__name__)
def home(request,context={},template_name=None):
    logger.info(str(request), exc_info=True)
    return render_to_response(template_name,context, context_instance=RequestContext(request))

I tried many settings.

like image 28
Guillermo Siliceo Trueba Avatar answered Sep 27 '22 21:09

Guillermo Siliceo Trueba