在 Django 上生成的错误电子邮件中添加哈希值
我们有一个 Web 应用程序,可以发送有关服务器错误的电子邮件,例如 django指定。不过,我们需要为每封电子邮件附加一个哈希值,以便我们可以更好地管理它们。
不幸的是,大多数讨论重新格式化这些电子邮件的文档都是针对 1.3 版本的。我们用的是1.2.7,无法升级。这似乎意味着 dictConfig() 示例不不适用于我们。
我发现了这种修补 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:
不幸的是,每次我们升级 django 时都必须重新应用此补丁。
在 django 1.2 中自定义电子邮件的最佳方法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不需要通过电子邮件进行回溯,则管理大量回溯的更好方法可能是 Django Sentry 。
If tracebacks via email are not a requirement, a better way to manage a high volume of tracebacks might be Django Sentry.