Using Sentry to log exceptions and logging messages from Django projects
How to setup Sentry log aggregator for Django projects with exception, logging and Celery tasks support
In this article I'll show you how to run Sentry on your local computer (basic setup) and how to configure Django and other parts of the Python code for optimal Sentry logging results.
Installing and configuring SentryInstallation is standard:
Under Ubuntu-based system I had to also install Python header files for
setproctitle compilation (dependency of sentry). I've installed
libpython-all-dev and that solved the problem. Note that current Sentry release (0.5.5) is not compatible with Django 1.5 and newer so if you have such version in your system use a virtualenv to install Sentry in it.
As the platform we pick the framework or language we are interested in. In the case of Django we will get a basic setup instruction after we create the project. Presented configuration covers basic exception logging only.
Configuring DjangoInstall raven at start:
Exception loggingHere is our test view: Which should show up in Sentry as well:
You can click on the exception title to get more details like the whole exception, stacktrace, Django user data etc.
Logging messages from logging module
Logging module is often use to log some events that did occur, while they shouldn't in general (either in the case of a catch exception or odd result that isn't a critical error for which exception would be suitable). Here is an example usage:Opening any of the two view won't log anything as there is no handler configured yet. Development server will write something like this: In Django (1.3 or newer) settings find LOGGING and replace it with:
This configuration is described on Sentry documentation. Whats worth checking is that "root" and "sentry" have "DEBUG" log level. They will log everything (usually you would send errors and warnings to sentry, and low important debug or info to some files etc.).
With new LOGGING configuration the logger will log to Sentry:You may notice that the logged event doesn't contain to much data. There is no request or stacktrace. Python logging supports passing a dictionary under extra argument. Sentry has some extended support for it. If you pass the request object under
requestkey it will log it nicely. If you pass
stackkey set to True it will add a stacktrace and you will be able to tell which of the two test views triggered the logged event (and more). You can also use SENTRY_AUTO_LOG_STACKS in settings to log stacktraces by default:
Logging from Celery tasks to SentryTo log exceptions from Celery task we have to add one extra logger to LOGGING: Which will cover exceptions. To make logging work you have to use it like so: Where "celery_task" is the function-task name.
To use the logging like in the views logging you can set CELERYD_HIJACK_ROOT_LOGGER to False:
Some other Python services may require their own logger. But that's all you need to get a nicely configured Sentry logging.