Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

turn off SQL logging while keeping settings.DEBUG?

Django logs SQL operations to an internal buffer (whether logging to file or not) when settings.DEBUG=True. Because I have long-running process that does a lot of DB operations, this causes my development-mode instances of the program to grow in memory consumption very quickly.

I would like to disable the internal SQL logging mechanism while leaving settings.DEBUG turned on for my development: is this possible?

Django version 1.3.0.

like image 817
kdt Avatar asked Oct 14 '11 13:10

kdt


People also ask

Can you debug a SQL query?

You can start the Transact-SQL debugger after you open a Database Engine Query Editor window. Then, you can run your Transact-SQL code in debug mode until you stop the debugger. You can set options to customize how the debugger runs. This feature works with SSMS version 17.9.

How do I enable SQL logging?

To enable temporary SQL logging:Go to > General Configuration > Logging and Profiling. Choose Enable SQL Logging.

Does SQL Server log queries?

SQL Statements are saved in a list written to a file and/or a database. The solution can delete the older SQL query history logs automatically. Additionally, you can monitor who and when executed any particular query, or receive other valuable information for research and analysis.


2 Answers

Yes, you can quiet the sql logging down by assigning a 'null handler' to the logger named 'django.db.backends'. I assume you use django's new dict-based logging setup? If so, this snippet ought to make it easy:

    ...     'handlers': {         'null': {             'level': 'DEBUG',             'class':'logging.NullHandler',             },     ...     'loggers': {         ... your regular logger 'root' or '' ....         'django.db.backends': {             'handlers': ['null'],  # Quiet by default!             'propagate': False,             'level':'DEBUG',             },     ... 

Update: look at Brian's answer, too. I understood "logging" to mean the irritating logging of every sql statement. Brian talks about the internal memory logging of every query (and I guess he's right :-)

like image 152
Reinout van Rees Avatar answered Oct 07 '22 01:10

Reinout van Rees


When settings.DEBUG is True, Django uses CursorDebugWrapper instead of CursorWrapper. This is what appends the queries to connection.queries and consumes memory. I would monkey-patch the connection wrapper to always use CursorWrapper:

from django.conf import settings from django.db.backends.base.base import BaseDatabaseWrapper from django.db.backends.utils import CursorWrapper  if settings.DEBUG:     BaseDatabaseWrapper.make_debug_cursor = lambda self, cursor: CursorWrapper(cursor, self) 

Place this in some file that gets imported early in your application.

Disabling logging like others suggest won't fix the problem, because CursorDebugWrapper still stores the queries in connection.queries even if logging is off.

like image 38
Brian from QuantRocket Avatar answered Oct 07 '22 01:10

Brian from QuantRocket