[Solved]-Django verbose request logging

17πŸ‘

βœ…

I can’t say I did an exhaustive search, but from what I can see at django/core/handlers/base.py of django 1.4.1, the django.request logger is indeed only used for warnings and error conditions (4xx/5xx).

That said, it’s trivial to write middleware that will do all manner of logging for you. Something very simple to get you started could be just:

from time import time
from logging import getLogger

class LoggingMiddleware(object):
    def __init__(self):
        # arguably poor taste to use django's logger
        self.logger = getLogger('django.request')

    def process_request(self, request):
        request.timer = time()
        return None

    def process_response(self, request, response):
        self.logger.info(
            '[%s] %s (%.1fs)',
            response.status_code,
            request.get_full_path(),
            time() - request.timer
        )
        return response
πŸ‘€Yaniv Aknin

Leave a comment