Package: django-errorlog
django-errorlogDjango reuseable app to collect the unexpcted exception then generate
comprehansive report just like what you get in debug mode and store in
databaseIntroductionDjango 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.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.Each error item have the field to record a. fixed b. vcs
version(support hg and git), you canignoreit after this bug has
been addressed. Then it will gone from theunfixed_errorlist.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 Logs2016-12-04: 0.1.0 Initial submit. Split the code from the online
project. Write the documents, and add the tests.Installpipinstalldjango-errorlogThen modify the settingsadderrorlogin the INSTALLED_APPSif you are using django>=1.10, inserterrorlog.middlewares.ErrorLogMiddlewarein theMIDDLEWARESat
the first line.(optional) if you have your django project live inside a VCS(hg or
git), setVCS_SYSTEM = "hg"orVCS_SYSTEM = "git"to enable
the erro rev tracking.Then dopython manage.py migrateto setup the database table.Then when your views get an 500 error, there will be a new log item
stored.Usagebuildin shell command>>>fromerrorlog.modelsimportError>>>Error.unfixed_errors{0:<Error:1-/test/2/-ValueError:A>,1:<Error:4-/test/1/-ValueError:B>}>>>error=Error.unfixed_errors[1]>>>error1:<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 logsDjango adminIf 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 UsageYou can use Error.log_exception to log one specific error in one
certain scope.fromerrorlog.modelsimportErrorwithError.log_exception("name",reraise=False):do_something_here()Ifreraise = 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.Ifreraise = False, then it will log the exception then stop
propogation and continue to run the following code. It’s as same as the
following codetry:do_something_here()exceptExceptionase:passHere is an example of how I using itwithError.log_exception("send_email_through_mailgun",reraise=False):response=requests.post(url,parms)content=response.contentstatus_code=response.status_codeifstatus_code!=200:raiseValueError("Mailgun failed")other_stuff()So that I can capture when the mailgun’s api return an error, and keep
the stuff going.