Learn Django

Configure Error Logging & Reporting

Django has a great debugging system that is used during development. This system is enabled when the DEBUG setting is set to True in the project settings file. When it's enabled errors are shown as problems are encountered. You've likely seen these error pages while working on this project.

However, DEBUG needs to be set to False in production so that sensitive information isn't leaked. This creates a challenge because you can no longer see issues as they happen. In addition, in production you aren't the only one who can generate errors; application users will also generate errors and you need a way to be notified of them. Luckily the developers of Django thought about this challenge and created a way for you to get error messages a different way.

In this lesson you'll learn how to setup the application to capture and report errors. This will require the configuration of four different components; Raven, Django, Heroku and Sentry. First, a utility called Raven must be installed and configured. Raven is used to capture errors from Django and send them to a third party reporting utility called Sentry. Second, Django must be configured to log errors and send them to the Raven client. Lastly, Heroku and Sentry need to be configured to to receive the errors from the Raven client.

1. Install Raven

The first step is to install Raven.

  1. Open your command prompt, activate the virtualenv, and run this command: $ pip install raven
  2. Then update the requirements file: $ pip freeze > requirements.txt
  3. Add 'raven.contrib.django.raven_compat', to installed_apps

2. Configure Django Logging

Next configure Django to log errors to the Raven client. You can learn more about Django logging by reading the official documentation. I highly recommend reading this article so that you have a firm background on how logging works.

Note that the below configure is logging WARNING level errors. If you want more detailed logging you can change the text from WARNING to INFO or DEBUG depending on how detailed you want. Note that the more detailed you get, the more impact it can have on application performance.

You can learn more about configuring Raven's Django/Python client at their site.

  1. Open settings.py
  2. Locate the logging section. Make it like so.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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': 'WARNING',
            'propagate': True,
        },
        'raven': {
            'level': 'WARNING',
            'handlers': ['sentry'],
            'propagate': False,
        },
        'sentry.errors': {
            'level': 'WARNING',
            'handlers': ['sentry'],
            'propagate': False,
        },
    }
}

Code Review

Lines 4-8: This sets up the log formatting. It determines the structure of data as it is written to a handler e.g. a log file.

Lines 9-19: This sets up two handlers. The first handler is Sentry; this configuration will ensure that all WARNING level errors will be sent to the Sentry service using the Raven client.

The second handler is the console is the equivalent to the screen. It can be accessed at the command line using the heroku logs --app <app name>.

Lines 20-36: This sets up three sources of logging information.

The first one is Django; this is a catch all source that will capture errors from Django's four loggers. Currently this is set up so that errors are only sent to the Sentry handler.

The second and third loggers are for Raven and Sentry, respectively. These will ensure that errors are reported if there are any errors with Raven or Sentry themselves.

3. Push the changes to Staging and Production

Now you can update the staging and production Heroku apps to have the new logging configuration.

Go through these steps for each environment:

1
2
3
4
(venv)$ git add .
(venv)$ git commit -m "installed Raven and configured error logging"
(venv)$ git push staging master
(venv)$ git push prod master

4. Configure Sentry App

Next, install Sentry on the Heroku staging and production apps.

  1. Open command prompt and activate the Sentry app on Heroku production
1
2
3
4
5
# get Heroku app names
(venv)$ git remote -v

(venv)$ heroku addons:add sentry --app <prod app>
(venv)$ heroku addons:add sentry --app <staging app>

4. Test the Configuration

To test that this is working you can generate an error and watch it appear on Sentry.

  1. Try to access an invalid URL of the production application
  2. Open Sentry for the production app
Track your progress with a free account