Django错误报告 - 如何知道哪个用户触发了错误? [英] Django Error Reporting - How to know which user triggered the error?

查看:115
本文介绍了Django错误报告 - 如何知道哪个用户触发了错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法可以自定义Django错误报告,所以当它发电子邮件给我,让我知道哪个用户触发了错误?

Is there a way I can customize Django error reporting so when it emails me it lets me know which user triggered the error?

我在Django 1.2中这很重要。

I'm in Django 1.2 if it matters.

非常感谢提前!

推荐答案

不想使用哨兵你可以使用这个简单的中间件来附加用户信息的错误邮件:

If you don't want to use sentry you can use this simple middleware to attache the user-infos to the error mail:

# 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 。即如果你把它放在项目根目录(你的settings.py中)的文件中间件中,你只需添加 middleware.ExceptionUserInfoMiddleware

You can simply put this class in a *.py file anywhere below your Django project and add a reference to MIDDLEWARE_CLASSES. I.e. if you put it in a file "middleware" in the project root (where your settings.py is) , you simply add middleware.ExceptionUserInfoMiddleware.

这篇关于Django错误报告 - 如何知道哪个用户触发了错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆