Created
December 24, 2010 04:31
-
-
Save skyl/753900 to your computer and use it in GitHub Desktop.
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
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': | |
logger.info('POST to %s: %s' % (request.get_full_path(), request.POST)) | |
if settings.DEBUG: | |
logger.debug('-------------------RESETTING QUERIES REQUEST-------------------------------') | |
db.reset_queries() | |
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())) | |
timesql=0.0 | |
duplicate = 0 | |
seen={} | |
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-------------------------------') | |
db.reset_queries() | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment