Login

SQL Log Middleware

Author:
joshua
Posted:
February 28, 2007
Language:
Python
Version:
Pre .96
Tags:
sql middleware log
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. SQL Log Middleware - with multiple databases by guglielmocelata 4 years, 5 months ago
  2. SQL Log Middleware w/query count & exec time by tobias 8 years, 3 months ago
  3. SQL Log To Console Middleware by davepeck 5 years, 11 months ago
  4. Color SQL logging middleware by moe 7 years, 6 months ago
  5. SQL Log Middleware + duplicates by guettli 8 years 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.