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 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.
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.
Incovinient to trace the errors, it's a email, hard to catoegorized, and hard to track the status.
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.
ignore
it after this bug has been addressed. Then it will gone from the unfixed_error
list.This Module has been running in my company's website for more than 1 year and helps to solved thousands of bugs.
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
errorlog
in the INSTALLED_APPSerrorlog.middlewares.ErrorLogMiddleware
in the MIDDLEWARES
at the first line.VCS_SYSTEM = "hg"
or VCS_SYSTEM = "git"
to enable the erro rev tracking.Then do python manage.py migrate
to setup the database table.
Then when your views get an 500 error, there will be a new log item stored.
>>> 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
"1"
>>> error.ignore() # this command ignore the whole 4 error logs
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.
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):
do_something_here()
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
try:
do_something_here()
except Exception as e:
pass
Here is an example of how I using it
with Error.log_exception("send_email_through_mailgun", reraise=False):
response=requests.post(url,parms)
content=response.content
status_code=response.status_code
if status_code!=200:
raise ValueError("Mailgun failed")
other_stuff()
So that I can capture when the mailgun's api return an error, and keep the stuff going.