Django 错误报告 - 如何知道哪个用户触发了错误?

发布于 2024-11-19 23:38:39 字数 110 浏览 4 评论 0原文

有没有一种方法可以自定义 Django 错误报告,以便当它向我发送电子邮件时,它可以让我知道哪个用户触发了错误?

如果重要的话,我使用的是 Django 1.2。

非常感谢!

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

I'm in Django 1.2 if it matters.

Much Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南城旧梦 2024-11-26 23:38:39

如果您不想使用哨兵,您可以使用这个简单的中间件将用户信息附加到错误邮件中:

# 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 即可。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文