Logging raw sql

84 views
Skip to first unread message

Dave Sluder

unread,
Mar 8, 2011, 11:04:01 PM3/8/11
to Django users
I need to log all insert/update/delete queries such that the database
could be rebuilt from the log. I know I can do this at the database
level, but I need to log selectively for certain models.
The statements captured from db.connection.queries still contain
parameters so they won't work.

Does anyone have any insight as to where I can intercept the raw query
if at all?

krzysiekpl

unread,
Mar 9, 2011, 7:35:19 AM3/9/11
to Django users
Hi, if you are using postgresql you can use logger_collector. Try
this: http://www.postgresql.org/docs/8.3/static/runtime-config-logging.html.

Dave Sluder

unread,
Mar 9, 2011, 12:57:59 PM3/9/11
to Django users
Thanks krzysiekpl, I actually have to use mysql. Having browsed
through the django source, I don't think there is anyway to intercept
the query unless it were done for example in mysqldb, since the
library actually generates the raw sql. I've discovered the
general_log to table feature of mysql 5.1 and am just executing a
query against that to filter the results.

Marwan Al-Sabbagh

unread,
Mar 10, 2011, 10:10:43 PM3/10/11
to django...@googlegroups.com
Hi,
  There are three different possible solutions you could investigate to do this logging on the django/python level:

1) use django's new logging feature that as per the docs will log "every SQL statement executed by a request". The beauty of this approach is that you can use python's excellent logging module an just add your own filter and handler to grab exactly what you want and log it the way you want. you can read more about it at http://docs.djangoproject.com/en/dev/topics/logging Keep in mind though this is a django 1.3 feature which looks to be released within a week.
2) check out django-debug-toolbar (http://robhudson.github.com/django-debug-toolbar/) which displays all SQL queries executed in a request, you could read through their code and see how they hook in and get that information
3) you could take the mysql database backend django.db.backends.mysql which seems to be a collection of classes and try and make your own custom backend that only extends certain classes that you need to be logged. It could be like a thin wrapper around django.db.backends.mysql

hope one of these solutions works for you.

cheers,
Marwan

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Reply all
Reply to author
Forward
0 new messages