[Solved]-Django Error Logging: Adding request header, body and user information


I think a complete solution to the logging problem you have is to implement a middleware. The middleware would be able to work with any kind of view implementation you have, irrespective of whether it is a class based view, function based view or APIView from DRF.

You can define a middleware for full logging. Make sure that you place the middleware appropriately after the authentication middleware –


In the log middleware, you would have access to the request and response. You can store request, the user (if authenticated) and all the META properties coming from the request via a logger, or you can even store it in a database if you want. Although, beware that storing in database comes at a cost. You can learn how to write a middleware by going through the Django middleware documentation.

import traceback

class LogMiddleware():
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
            return self.get_response(request)
            if request.user.is_authenticated():
                # Log the user
            path = request.get_full_path() # Get the URL Path
            tb = traceback.format_exc() # Get the traceback
            meta = request.META # Get request meta information
            # Log everything
            raise  # Raise exception again after catching

You can read about all the meta attributes present from the django documentation of HttpRequest. Let me know if you need any clarification on this.


I would use a decorator here. Cut straight to the code…

import logging
from functools import wraps
from django.http import HttpResponse, HttpRequest

                    format='%(asctime)s: %(message)s')
log = logging.getLogger(__name__)

def log_exceptions(wrapped):
    def wrapper(*args, **kwargs):
            return wrapped(*args, **kwargs)
            # log and re-raise
            request = args[0] if len(args) > 0 and isinstance(args[0], HttpRequest) else None
            msg = ("\nuser.id/email: {}/{}\nMETA: {}...\nbody: {}"
                           getattr(request.user, 'email','?'),
                   if request
                   else "not a HttpRequest")
    return wrapper

def random_view(request):
    raise ValueError("simulate a crash")
    if request.user.is_authenticated() and request.user.is_active:
        return HttpResponse('hi')
         # generic view code goes here.
        return HttpResponse(status=401)

and errors.log should capture something like

2017-06-27 20:48:09,282: 
user.id/email: 1/test@domain.com
META: {'SESSION_MANAGER': 'local/acb:@/tmp/.ICE-unix/4255,unix/acb:/tmp/.ICE-unix/4255...
body: b''
Traceback (most recent call last):
  File "/home/rod/pyves/rangetest/rangetest/data/views.py", line 14, in wrapper
    return wrapped(*args, **kwargs)
  File "/home/rod/pyves/rangetest/rangetest/data/views.py", line 31, in random_view
    raise ValueError("simulate a crash")
ValueError: simulate a crash

Note, you’ll also probably see the Django crash logging in your errors.log as well. You might split the logs to separate files using Django’s well documented, but nonetheless complex logging config

👤rm -rf

Leave a comment