Logging SQL queries

313 views
Skip to first unread message

candlerb

unread,
May 24, 2011, 10:23:39 AM5/24/11
to Django users
I have Django 1.3 installed, and would like the built-in 'runserver'
to show me the SQL queries it is executing.

I found this thread:
http://groups.google.com/group/django-users/browse_thread/thread/9ed3ea72afc02ee8/a5df8c062640d063?show_docid=a5df8c062640d063&fwc=1&pli=1

However, after reading the Django logging docs, I can't see how to
glue this together. I have the following in settings.py:

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %
(process)d %(thread)d %(message)s'
},
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler'
},
'console':{
'level':'DEBUG',
'class':'logging.StreamHandler',
'formatter': 'simple'
},
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
'django.db.backend': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': True,
},
}
}

This config is accepted, but runserver's console output still only
shows one line for each GET or POST, and does not include any SQL
queries.

It would be helpful if I could work out how the django.request logger
is plumbed into console output, but I can't see how it is at the
moment (notice that the config above only sends ERROR to mail_admins)

Anyway, I'm a bit stumped - does anyone have a working example of this
they can share?

Many thanks,

Brian.

Shawn Milochik

unread,
May 24, 2011, 12:28:28 PM5/24/11
to django...@googlegroups.com
I recommend logging to a file.

Here's a handler:

'django_log_file': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': '/tmp/myproject_django.log',
'formatter': 'verbose',
'backupCount': 50,
'maxBytes': 2 ** 20,
},


Here's a logger:

'django':{
'handlers': ['django_log_file'],

candlerb

unread,
May 24, 2011, 3:25:48 PM5/24/11
to Django users
On May 24, 5:28 pm, Shawn Milochik <sh...@milochik.com> wrote:
> Here's a handler:
...
> Here's a logger:

Thank you. I tried this and it still didn't log my SQL queries. Then I
went through the documentation again, and found that I should have
configured "django.db.backends" instead of "django.db.backend".

It's working now. Sorry for the noise - it would have been nice if my
junk config had provoked a warning though :-)

Regards,

Brian.

Shawn Milochik

unread,
May 24, 2011, 3:31:15 PM5/24/11
to django...@googlegroups.com
On 05/24/2011 03:25 PM, candlerb wrote:
> Thank you. I tried this and it still didn't log my SQL queries. Then I
> went through the documentation again, and found that I should have
> configured "django.db.backends" instead of "django.db.backend".
>
> It's working now. Sorry for the noise - it would have been nice if my
> junk config had provoked a warning though :-)
>
> Regards,
>
> Brian.
>

There won't be any warnings for that because nothing's wrong. If you
read up on how the logging module works it'll make sense. In short, you
can give any names you like (a lot of people just use __file__ to use
the current filename). The logging module then treats dotted names like
a hierarchy. So what you entered was a completely legitimate name -- it
just didn't happen to match the name the Django team used when they
created their logger instance.


Reply all
Reply to author
Forward
0 new messages