Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging Django SQL queries with DEBUG set to False

I know that it is possible to get all the SQL queries that were run for the current request/response when DEBUG is on by looking at connection.queries. The django-debug-toolbar also helps a lot on development.

The problem is that my production server is under high load and I would like to log the queries that are being executed for each view so I can optimize the pages that are creating more queries first.

Is it possible to do that without modifying my database driver?

like image 995
jbochi Avatar asked Aug 24 '11 19:08

jbochi


People also ask

How do I log a query in Django?

Logging all SQL queries LOGGING = { ... 'handlers': { ... 'console': { 'level': 'DEBUG', 'class': 'logging. StreamHandler', }, ... }, ... } Next, you'll need to add a logger that logs to this handler: LOGGING = { ... 'loggers': { ... 'django.

How do you disable verbose error handling and logging in Django?

If you don't want to configure logging at all (or you want to manually configure logging using your own approach), you can set LOGGING_CONFIG to None . This will disable the configuration process for Django's default logging.

Can you debug a SQL query?

Use the Client Statistics feature to debug query performance Analyzing the Client Statistics such as application profile, network, and execution time statistics enables you to determine whether your query has improved the performance. This one can also help debug query performance issues.


1 Answers

In Django 1.3, I see class BaseDatabaseWrapper in django/db/backends/__init__.py has an attribute use_debug_cursor. This class is the wrapper for the django.db.connection object that represents the default database connection (docs). Seems like setting this attribute to true would cause Django to use a CursorDebugWrapper from django/db/backends/util.py, which logs all queries it executes, instead of a CursorWrapper, which does not.

use_debug_cursor is not present in Django 1.2, which is what I have installed on this machine at the moment, so I can't test it right now. If I have a few minutes, I'll try to get a dummy 1.3 project started to test this out...I may well be in over my head here!

use_debug_cursor was renamed to force_debug_cursor in Django 1.8

like image 143
sandinmyjoints Avatar answered Sep 28 '22 02:09

sandinmyjoints