Enable django registered user in django error traceback

What is an easy way to include username, first and last name and e-amil in django Traceback error.

I know that the way to create my own error report is :

  • Create a new class that inherits from django.views.debug.SafeExceptionReporterFilter
  • Set DEFAULT_EXCEPTION_REPORTER_FILTER

But which method a should rewrite to get a trace with this information?

I would like my t-byte to look like this:

Traceback (most recent call last):

 File "/usr...o/core/handlers/base.py", line 89, in get_response
   response = middleware_method(request)

 File "/.../g...ap/utils/middleware.py", line 23,...
   if elapsedTime.min > 15:

TypeError: can't compare datetime.timedelta to int

Logged user information:
User:    pepito
name:    Pepito Grillo
e-mail:  grillo@peppeto.com
+5
source share
2 answers

I did this with Custom Middleware. I am not sure if this is the best answer, but I decided it for my project.

settings.py:

MIDDLEWARE_CLASSES = (
    ...
    'utilities.custom_middleware.CustomMiddleware',
    ...
)

utility / custom_middleware.py:

from utilities.request import AddRequestDetails

class CustomMiddleware(object):
"""
    Adds user details to request context during request processing, 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 request to add some variables to it.
        """

        # Add other details about the user to the META CGI variables.
        try:
            if request.user.is_authenticated():
                AddRequestDetails(request)
                request.META['AUTH_VIEW_ARGS'] = str(view_args)
                request.META['AUTH_VIEW_CALL'] = str(view_func)
                request.META['AUTH_VIEW_KWARGS'] = str(view_kwargs)
        except:
            pass

utility / request.py:

def AddRequestDetails(request):
"""
    Adds details about the user to the request, so any traceback will include the 
    details. Good for troubleshooting; this will be included in the email sent to admins 
    on error. 
"""
if request.user.is_anonymous():
    request.META['AUTH_NAME'] = "Anonymous User"
    request.META['AUTH_USER'] = "Anonymous User"
    request.META['AUTH_USER_EMAIL'] = ""
    request.META['AUTH_USER_ID'] = 0
    request.META['AUTH_USER_IS_ACTIVE'] = False
    request.META['AUTH_USER_IS_SUPERUSER'] = False
    request.META['AUTH_USER_IS_STAFF'] = False
    request.META['AUTH_USER_LAST_LOGIN'] = ""
else:
    request.META['AUTH_NAME'] = str(request.user.first_name) + " " + str(request.user.last_name)
    request.META['AUTH_USER'] = str(request.user.username)
    request.META['AUTH_USER_EMAIL'] = str(request.user.email)
    request.META['AUTH_USER_ID'] = str(request.user.id)
    request.META['AUTH_USER_IS_ACTIVE'] = str(request.user.is_active)
    request.META['AUTH_USER_IS_SUPERUSER'] = str(request.user.is_superuser)
    request.META['AUTH_USER_IS_STAFF'] = str(request.user.is_staff)
    request.META['AUTH_USER_LAST_LOGIN'] = str(request.user.last_login)
+7

( django 1.5)

settings.py:

MIDDLEWARE_CLASSES = (
    ...
    'utilities.custom_middleware.UserTracebackMiddleware',
    ...
)

custom_middleware.py:

class UserTracebackMiddleware(object):
    """
    Adds user to request context during request processing, so that they
    show up in the error emails.
    """
    def process_exception(self, request, exception):
        if request.user.is_authenticated():
            request.META['AUTH_USER'] = unicode(request.user.username)
        else:
            request.META['AUTH_USER'] = "Anonymous User"

,

+8

All Articles