Log database queries in Django
While I’m on the topic of logging in Django, it makes me sad and a little bit grumpy that when you take Django out of the box, it doesn’t default to showing you what it’s doing with your database.
Here’s a LOGGING configuration that takes care of that, and sends everything from django.db.backends to a file in the project’s root directory during development:
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"db_backends_file": {
"class": "logging.FileHandler",
"filename": BASE_DIR / "db_backends.log"
}
},
"loggers": {
"django.db.backends": {
"level": "DEBUG",
"handlers": ["db_backends_file"]
}
}
}
It’s important to note that django.db.backends only generates output when the DEBUG setting is True.