How can I include the request.User details in Django's traceback email for a production site

若如初见. 提交于 2019-12-19 04:04:07

问题


I would like to include the contents of request.user in the context details emailed to the site admins when an error occurs, as well as the traceback and request.GET/POST/COOKIES/META

Any help appreciated.


回答1:


Because process_exception middleware gets passed the request object, you can add whatever info you like to request.META

class ErrorMiddleware(object):
    """
    Alter HttpRequest objects on Error
    """

    def process_exception(self, request, exception):
        """
        Add user details.
        """
        request.META['USER'] = request.user.username



回答2:


Make a middleware that has a process_exception method. http://docs.djangoproject.com/en/dev/topics/http/middleware/#process-exception

import sys
import traceback
from django.conf import settings
from django.core.mail import mail_admins

class ProcessExceptionMiddleware(object):
    def process_exception(self, request, exception):
        if not settings.DEBUG:
            msg = '\n\n'.join([request.user, request.GET, request.POST, \
                request.COOKIES, request.META, traceback.format_exc(*sys.exc_info())])

            mail_admins("Error!", msg)

I hope that gives you some ideas!



来源:https://stackoverflow.com/questions/4945483/how-can-i-include-the-request-user-details-in-djangos-traceback-email-for-a-pro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!