Adding a hash to the generated error emails on Django

195 Views Asked by At

We have a web application that sends out emails on server errors like django specifies. We need to attach a hash to each email, though, so that we can better manage them.

Unfortunately, the majority of the documentation that discusses reformatting these emails is for version 1.3. We use 1.2.7, and can't upgrade. It seems like this means that the dictConfig() examples don't apply to us.

I have found this method of patching django:

diff --git a/django/django/core/handlers/base.py b/django/django/core/handlers/base.py
index 45f8445..1605549 100644
--- a/django/django/core/handlers/base.py
+++ b/django/django/core/handlers/base.py
@@ -1,4 +1,4 @@
-import sys 
+import sys, md5 

 from django import http
 from django.core import signals
@@ -170,7 +170,11 @@ class BaseHandler(object):
             request_repr = repr(request)
         except:
             request_repr = "Request repr() unavailable"
-        message = "%s\n\n%s" % (self._get_traceback(exc_info), request_repr)
+        tb=self._get_traceback(exc_info)
+        # generate an md5 hash from this as a fingerprint
+        hash=md5.new(tb)
+        fingerprint='fingerprint: %s' % hash.hexdigest()
+        message = "%s\n\n%s\n\n%s" % (tb, fingerprint, request_repr)
         mail_admins(subject, message, fail_silently=True)
         # If Http500 handler is not installed, re-raise last exception
         if resolver.urlconf_module is None:

Unfortunately this patch has to be re-applied each time we upgrade django.

What's the best way to customize emails in django 1.2?

1

There are 1 best solutions below

1
On

If tracebacks via email are not a requirement, a better way to manage a high volume of tracebacks might be Django Sentry.