Login

SQL Log Middleware

Author:
joshua
Posted:
February 28, 2007
Language:
Python
Version:
Pre .96
Score:
4 (after 4 ratings)

This middleware will add a log of the SQL queries executed at the bottom of every page. You can (should) use BeautifulSoup to place this in a specific location.

Note: If you serve non-html content, it would be wise to do a mimetype check.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from django.db import connection
from django.template import Template, Context

class SQLLogMiddleware:

    def process_response ( self, request, response ): 
   
        t = Template('''
            <ul class="sqllog">
                {% for sql in sqllog %}
                    <li>{{ sql.sql }}</li>
                {% endfor %}
            </ul>
        ''')

        response.content = "%s%s" % ( response.content, t.render(Context({'sqllog':connection.queries})))

More like this

  1. Treat empty value on DateRangeFilter as a filter for non filled dates by kmmbvnr 1 month ago
  2. Django Settings Assignment Expressions aka Walrus Operator example by webology 2 months, 1 week ago
  3. codigo alto nivel by MrRocklion 4 months ago
  4. Load template from specific app by Krzysiek555 4 months, 3 weeks ago
  5. PostgreSQL JSON subqueries by dolamroth 4 months, 3 weeks ago

Comments

rlawson (on March 30, 2007):

this worked great but I had to add the following line to return a response

...

response.content = "%s%s" % ( response.content, t.render(Context({'sqllog':connection.queries})))

return response

#

guettli (on July 27, 2007):

I improved Snippet 161 in Snippet 344.

It counts duplicated SQL queries.

#

Please login first before commenting.