11

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?

Maximilian Kindshofer
  • 2,753
  • 3
  • 22
  • 37
  • Generally DEBUG logging is not useful in Sentry. We push back pretty heavily when someone says they want this, because usually they misunderstand how Sentry works. If you're fine w/ the limitations of Sentry, or you have a non-standard case where you use the DEBUG level for actually important error-type data, then this is ok. – David Cramer Jan 11 '16 at 20:15
  • Ok thanks for the clarification - I tried to follow https://docs.getsentry.com/hosted/clients/python/integrations/django/ and while exception handling worked really nice out of the box the integration with logging didn't worked with the setup - It is totally possible that I misunderstood the section and / or it is not the main usease but I thought it would be nice if the config in the config turned out working for me – Maximilian Kindshofer Jan 11 '16 at 20:36

1 Answers1

15

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!')
Alex Morozov
  • 5,823
  • 24
  • 28
  • Thanks for you answer - but somehow it still doens't work I et the logger to 'raven' but it dosn't get its message to sentry – Maximilian Kindshofer Jan 11 '16 at 19:19
  • Sorry, but the above config works just fine in my environment. I've put together a gist just from the real code, take a look: https://gist.github.com/alexmorozov/b1fd5a8cd292b64998c3 . Maybe your settings somehow haven't got reloaded? – Alex Morozov Jan 11 '16 at 19:57
  • Fantastic! Let's sort out what was the reason to make the answer complete. Was it the INSTALLED_APPS addition or a `root` section in `LOGGING`? – Alex Morozov Jan 11 '16 at 20:15
  • I think it was the section in the LOGGING and the name in getLogger, than can it take some time till the message is transfered to sentry. Than the loglevel must be at least has high as the level specified at the handler. And nothing of this is mentioned in the docs (or I didn't found it) – Maximilian Kindshofer Jan 11 '16 at 20:26