Skip to content

Instantly share code, notes, and snippets.

@skyl skyl/gist:753900
Created Dec 24, 2010

What would you like to do?
from django import db
from django.db import connection
from django.conf import settings
class LoggingMiddleware(object):
def process_request(self, request):
"""Logs the POST (and resets the queries if DEBUG)"""
if request.method == 'POST':'POST to %s: %s' % (request.get_full_path(), request.POST))
if settings.DEBUG:
logger.debug('-------------------RESETTING QUERIES REQUEST-------------------------------')
def process_response(self, request, response):
"""Logs the SQL if DEBUG is True"""
if not settings.DEBUG:
return response
logger.debug('=== %s ===BEGIN SQL LOGGING BEFORE RETURN RESPONSE==============' % (request.get_full_path()))
duplicate = 0
for q in connection.queries:
time = float(q['time'])
timesql += time
sql = q["sql"]
count, total_time = seen.get(sql, (0,0.0))
if count:
duplicate += 1
seen[sql] = (count + 1, total_time + time)
for i, (sql,res) in enumerate(seen.iteritems()):
logger.debug('\n#%s ran %s time(s), taking %s ms\n%s\n\n' % (i, res[0], res[1]*1000, sql))
logger.debug('+++++ %s queries +++++' % len(connection.queries))
logger.debug('::::: %s unique ::::::' % len(seen))
logger.debug('TOTAL TIME FOR SQL: %s' % timesql)
logger.debug('TOTAL DUPES: %s' % duplicate)
logger.debug('-------------------RESETTING QUERIES RESPONSE-------------------------------')
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.