Created
June 2, 2010 14:01
-
-
Save jemerick/422401 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
### Appending URL to SQL query for logging | |
via http://chris-lamb.co.uk/2010/06/01/appending-request-url-sql-statements-django/ | |
Now your SQL statements look like: | |
SELECT "auth_user"."id", [..] WHERE "auth_user"."id" = 1 -- /login | |
This allows you to quickly go from a misbehaving query in your production environment to the view that is executing it. |
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
import sys | |
from django.http import HttpRequest | |
from django.db.backends import BaseDatabaseWrapper, util | |
class URLCursor(util.CursorDebugWrapper): | |
def execute(self, sql, *args): | |
f = sys._getframe() | |
while f: | |
request = f.f_locals.get('request') | |
if isinstance(request, HttpRequest): | |
sql += ' -- %s' % repr(request.path)[2:-1].replace('%', '%%') | |
break | |
f = f.f_back | |
return self.cursor.execute(sql, *args) | |
fn = BaseDatabaseWrapper.cursor | |
BaseDatabaseWrapper.cursor = lambda self: URLCursor(fn(self), self) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment