Log Database Queries

A snippet showing how to log database queries in Django.


# settings.py

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {
        "console": {"class": "logging.StreamHandler"},
    },
    "loggers": {
        "django.db.backends": {
            "handlers": ["console"],
            "level": "DEBUG",
            "propagate": False,
        },
    },
}
      
Explanation:
  • Use in development; avoid verbose SQL logging in production environments.
  • SQL queries (with timings) are printed to the console when DEBUG logging is enabled for django.db.backends.
  • Category Logging
  • Total Views 272
  • Last Modified 11 July, 2026
  • Tags #logging #database #queries #debug
Previous snippet
Console Logging Example
Next snippet
Custom Log Formatter
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.