Skip to content

Instantly share code, notes, and snippets.

@shangxiao
Created September 11, 2017 11:08
Show Gist options
  • Save shangxiao/3e50405351f500ea6ee5d478c7614798 to your computer and use it in GitHub Desktop.
Save shangxiao/3e50405351f500ea6ee5d478c7614798 to your computer and use it in GitHub Desktop.
FunkyBob's debug middleware
import time
from django.conf import settings
from django.db import connection
class DebugMiddleware:
def __init__(self):
import logging
self.log = logging.getLogger(__name__)
def process_request(self, request):
request.start_time = time.time()
def process_response(self, request, response):
if getattr(settings, 'DEBUG_QUERIES', False):
for q in connection.queries:
self.log.info(q)
self.log.info('Query Count: %d (%s s)', len(connection.queries), sum([float(q['time']) for q in connection.queries]))
self.log.info('View time: %s s', time.time() - request.start_time)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment