Created
September 11, 2017 11:08
-
-
Save shangxiao/3e50405351f500ea6ee5d478c7614798 to your computer and use it in GitHub Desktop.
FunkyBob's debug middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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