【发布时间】:2011-07-13 12:30:19
【问题描述】:
有没有一种方法可以自定义 Django 错误报告,所以当它给我发电子邮件时,它会让我知道哪个用户触发了错误?
如果重要的话,我在 Django 1.2 中。
提前非常感谢!
【问题讨论】:
标签: python django django-middleware django-email django-errors
有没有一种方法可以自定义 Django 错误报告,所以当它给我发电子邮件时,它会让我知道哪个用户触发了错误?
如果重要的话,我在 Django 1.2 中。
提前非常感谢!
【问题讨论】:
标签: python django django-middleware django-email django-errors
如果您不想使用 sentry,您可以使用这个简单的中间件将用户信息附加到错误邮件中:
# source: https://gist.github.com/646372
class ExceptionUserInfoMiddleware(object):
"""
Adds user details to request context on receiving an exception, so that they show up in the error emails.
Add to settings.MIDDLEWARE_CLASSES and keep it outermost(i.e. on top if possible). This allows
it to catch exceptions in other middlewares as well.
"""
def process_exception(self, request, exception):
"""
Process the exception.
:Parameters:
- `request`: request that caused the exception
- `exception`: actual exception being raised
"""
try:
if request.user.is_authenticated():
request.META['USERNAME'] = str(request.user.username)
request.META['USER_EMAIL'] = str(request.user.email)
except:
pass
您可以简单地将此类放在 Django 项目下方的任何位置的 *.py 文件中,并添加对 MIDDLEWARE_CLASSES 的引用。 IE。如果您将它放在项目根目录(您的 settings.py 所在的位置)的“中间件”文件中,您只需添加 middleware.ExceptionUserInfoMiddleware。
【讨论】:
【讨论】: