Menu Close

How to log all SQL queries with Python Django?

To log all SQL queries with Python Django, we can set the LOGGING variable in settings.py.

For instance, we write

LOGGING = {
    'version': 1,
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        }
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
        }
    },
    'loggers': {
        'django.db.backends': {
            'level': 'DEBUG',
            'handlers': ['console'],
        }
    }
}

to set LOGGING to a dictionary with loggers set to a dictionary with level set to DEBUG and handlers set to ['console'] to log SQL queries to the console.

Posted in Python, Python Answers