python - Django log queries with long execution time -
is there way log db queries long execution time django app?
here's middleware logs queries execution time longer 1 second:
from django.db import connection import logging long_query_time_sec = 1 class longquerylogmiddleware: def process_response ( self, request, response ): q in connection.queries: if float(q['time']) >= long_query_time_sec: logging.warning("found long query (%s sec): %s", q['time'], q['sql']) return response
notice because connection.queries
process-wide, queries can logged twice. can solved running following after @ end of function:
from django.db import reset_queries reset_queries()
Comments
Post a Comment