Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including Local Variables in Django Error Emails

I have a site built in Django.

When an error occurs on the production site, Django automatically sends a stack trace to the email addresses listed in the ADMINS list in settings.py.

I would like this stack trace to include local variables for each stack frame (like the standard stack trace does when the site is in debug mode).

Any ideas?

Thanks!

like image 430
bbrame Avatar asked Mar 22 '13 13:03

bbrame


People also ask

How to handle errors in Django?

When DEBUG is False , Django will email the users listed in the ADMINS setting whenever your code raises an unhandled exception and results in an internal server error (strictly speaking, for any response with an HTTP status code of 500 or greater). This gives the administrators immediate notification of any errors.

How does Django fix internal server error?

As first step, enable DEBUG mode in Django settings module to see if that fixes the issue or at least causes a description of the error in the browser. It is quite common to get a 500 error response delivered back from Django with no record of the exception being logged.


1 Answers

It's really simple to set this up. Just put 'include_html': True in the logging config of whichever handler is sending error emails for you.

For example (this is the default logging handler aside from the "include_html" line):

'handlers': {
    'mail_admins': {
        'level': 'ERROR',
        'filters': ['require_debug_false',],
        'class': 'django.utils.log.AdminEmailHandler',
        'include_html': True
    }
}

This includes an html attachment in the error email with the contents of the error page you get when DEBUG=True. The Django docs have a few more details and a note about security.

You could also look at setting up a logging handler which uses subclasses of django.utils.log.AdminEmailHandler and django.views.debug.ExceptionReporter if you need to customize things a lot more.

like image 74
Kevin Avatar answered Oct 07 '22 22:10

Kevin