Skip to main content

Django reuseable app to collect the unexpcted exception then generate comprehansive report just like what you get in debug mode and store in database

Project description


Django reuseable app to collect the unexpcted exception then generate comprehansive report just like what you get in debug mode and store in database


Django has it’s own error handling machanism, which will send a email to the admin address configed in the settings. It works but there are some shortage.

  1. The stack trace include in the email is as same as what you can see in console. It doesn’t contains the varible value which can be very useful to debug.

  2. Incovinient to trace the errors, it’s a email, hard to catoegorized, and hard to track the status.

  3. Some times one same error will bring you thousands of emails if this api happens to be visit a lot. You will waste a lot of time to find the different errors from the error happens most.

This module solves these problem in the following way.

  1. We are love the Django buildin debug 500 page, it’s contains almost all the information we need to debug, like the request infomation, the user, the settings, the stack trace with local vars, etc. So what we do, is to have a middleware to capture the unhandle exception then simply invoke the Django buildin reporter class to generate the full html report of the exception, then store in the database.

  2. Each error item have the field to record a. fixed b. vcs version(support hg and git), you can ignore it after this bug has been addressed. Then it will gone from the unfixed_error list.

  3. Errors will be categoried by the type of exception and the location where the exception been raised (location means the python file path and the method name). So in most case, same error that happened multiple times will be showed only once but with the count of how many times it’s happend. Then when the error been ignore, all the same error will been marked as ignored.

This Module has been running in my company’s website for more than 1 year and helps to solved thousands of bugs.

Change Logs

2016-12-04: 0.1.0 Initial submit. Split the code from the online project. Write the documents, and add the tests.


pip install django-errorlog

Then modify the settings

  1. add errorlog in the INSTALLED_APPS

  2. if you are using django>=1.10, insert errorlog.middlewares.ErrorLogMiddleware in the MIDDLEWARES at the first line.

  3. (optional) if you have your django project live inside a VCS(hg or git), set VCS_SYSTEM = "hg" or VCS_SYSTEM = "git" to enable the erro rev tracking.

Then do python migrate to setup the database table.

Then when your views get an 500 error, there will be a new log item stored.


buildin shell command

>>> from errorlog.models import Error
>>> Error.unfixed_errors
{0: <Error:     1 - /test/2/ - ValueError: A>,
 1: <Error:     4 - /test/1/ - ValueError: B>}
>>> error = Error.unfixed_errors[1]
>>> error
 1: <Error:     4 - /test/1/ - ValueError: B>
>>> # in this repr, the first number is the index to make it easy to select;
>>> # the second number 4 is the the count of the same error happened;
>>> # /test/1/ is the uri of the api;
>>> # ValueError is the exception type;
>>> # B is the args in the exception.
>>> error.vcs_rev # the git/hg version of error, for hg, it's the incremental number that is orderable
>>> error.ignore() # this command ignore the whole 4 error logs

Django admin

If you use django buildin admin, you should be able to find the Error in the home page.

If you want to see the html error report, you need to build the view youself to transfer the error_html to the browser.

Advance Usage

You can use Error.log_exception to log one specific error in one certain scope.

from errorlog.models import Error
with Error.log_exception("name", reraise=False):

If reraise = True, then after being loged, the exception will keep raising out. Caution, if you have database atomic open, since unhandle error will make django to rollback the transaction, so this log will also been rollbacked.

If reraise = False, then it will log the exception then stop propogation and continue to run the following code. It’s as same as the following code

except Exception as e:

Here is an example of how I using it

with Error.log_exception("send_email_through_mailgun", reraise=False):,parms)
    if status_code!=200:
        raise ValueError("Mailgun failed")

So that I can capture when the mailgun’s api return an error, and keep the stuff going.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-errorlog-0.1.1.tar.gz (13.3 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page