Categories
Python Answers

How to log all SQL queries with Python Django?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *