在Django错误报告中报告用户

时间:2021-03-15 16:56:24

I use Django's built-in feature for sending me error reports every time there's a 500 on the server. I use the include_html feature to get the richer version.

每次服务器上有500个错误报告时,我都使用Django的内置特性向我发送错误报告。我使用include_html特性来获得更丰富的版本。

I'd really like the logged-in user, if one is logged in, to be sent as part of the report. I know I can deduce it myself using the session id, but I'd really prefer it be sent so I won't have to look it up every time. How can I do that?

我非常希望登录的用户(如果有用户登录的话)作为报告的一部分被发送出去。我知道我可以用会话id来推断它,但我更希望它被发送,这样我就不用每次都去查了。我怎么做呢?

1 个解决方案

#1


1  

You need to make use of Custom Error Reports. settings.py You'll need to make your own exception reporter class. I haven't used it with the include_html feature though.

您需要使用自定义错误报告。设置。py,您需要创建自己的异常报告类。我还没有在include_html特性中使用它。

This is an example of what you'll need to do.

这是您需要做的一个示例。

from django.shortcuts import render
from django.views.debug import SafeExceptionReporterFilter


class CustomExceptionReporterFilter(SafeExceptionReporterFilter):

    def get_request_repr(self, request):
        result = u"User Info: \n"
        if request.user.is_authenticated():
            user = request.user
            result += u"\nUser Id: {}".format(user.id)
        else:
            result += u"\nUser Id: Not logged in"
        result += super(CustomExceptionReporterFilter, self).get_request_repr(request)
        return result

#1


1  

You need to make use of Custom Error Reports. settings.py You'll need to make your own exception reporter class. I haven't used it with the include_html feature though.

您需要使用自定义错误报告。设置。py,您需要创建自己的异常报告类。我还没有在include_html特性中使用它。

This is an example of what you'll need to do.

这是您需要做的一个示例。

from django.shortcuts import render
from django.views.debug import SafeExceptionReporterFilter


class CustomExceptionReporterFilter(SafeExceptionReporterFilter):

    def get_request_repr(self, request):
        result = u"User Info: \n"
        if request.user.is_authenticated():
            user = request.user
            result += u"\nUser Id: {}".format(user.id)
        else:
            result += u"\nUser Id: Not logged in"
        result += super(CustomExceptionReporterFilter, self).get_request_repr(request)
        return result