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 5 years, 6 months ago
  2. SQL Log Middleware w/query count & exec time by tobias 9 years, 4 months ago
  3. SQL Log To Console Middleware by davepeck 7 years ago
  4. Color SQL logging middleware by moe 8 years, 7 months ago
  5. SQL Log Middleware + duplicates by guettli 9 years, 1 month ago

Comments

rlawson (on March 30, 2007):
<p>this worked great but I had to add the following line to return a response</p> <p>...</p> <p>response.content = "%s%s" % ( response.content, t.render(Context({'sqllog':connection.queries})))</p> <p>return response</p>

#

guettli (on July 27, 2007):
<p>I improved Snippet 161 in Snippet 344.</p> <p>It counts duplicated SQL queries.</p>

#

Please login first before commenting.