Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging in Django with raven and sentry

I may be a bit late on the train, but I wanted to use Sentry and Raven for logging in Django.

I set up sentry and raven to the point, where I ran the test for raven and it works.

So now I want to send my debug messages over to sentry, but how would I do this?

settings.py

RAVEN_CONFIG = {
    'dsn': 'http://code4@mydomain:9000/2',
    # If you are using git, you can also automatically configure the
    # release based on the git info.
    'release': raven.fetch_git_sha(BASE_DIR),
}

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
    },
    'handlers': {
        'sentry': {
            'level': 'WARNING',
            'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
        },
        'console': {
            'level': 'WARNING',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        }
    },
    'loggers': {
        'django': {
            'handlers': ['sentry'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'raven': {
            'level': 'DEBUG',
            'handlers': ['sentry'],
            'propagate': False,
        },
        'sentry.errors': {
            'level': 'DEBUG',
            'handlers': ['sentry'],
            'propagate': False,
        },
    }
}

view.py

import logger
from raven.contrib.django.models import get_client


client = get_client()
client.captureException()

logger = logging.getLogger(__name__)



def my_view(request):

   [...]
    logger.error('There was some crazy error', exc_info=True, extra={
    # Optionally pass a request and we'll grab any information we can
    'request': request,
    })
    [...]

At this point it just logs errors and exceptions but wont send me this error message...

How is the right way to use raven and sentry? The docs are totaly not helping and my google foo left me also. Any tips or helpfull tutorials?

like image 675
Maximilian Kindshofer Avatar asked Jan 11 '16 18:01

Maximilian Kindshofer


People also ask

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.

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.


1 Answers

You have 3 loggers defined: django, raven and sentry.errors. When you call logging.getLogger(__name__) you actually create a "throw-away" one, because your ___name__ doesn't match any of above.

You should either use the raven logger...

logger = logging.getLogger('raven')
logger.debug('Hola!')

...or setup your own:

LOGGING = {
    # ...
    'loggers': {
        # ...
        'yourapp': {
            'level': 'debug',
            'handlers': ['sentry'],
            'propagate': False,
        },
    }
}

and then in yourapp/views.py:

logger = logging.getLogger(__name__)
logger.debug('Hola!')
like image 74
Alex Morozov Avatar answered Oct 16 '22 01:10

Alex Morozov