Home > Article > Backend Development > How to Log SQL Queries Comprehensively in a Django Application?
To comprehensively log all SQL queries executed by a Django application, including those generated by the admin site, follow these steps:
<code class="python">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'], } } }</code>
<code class="python">'handlers': { 'file': { 'level': 'DEBUG', 'filters': ['require_debug_true'], 'class': 'logging.FileHandler', 'filename': 'all-sql.log', } },</code>
With this configuration, all SQL queries executed by your Django application will now be logged to the specified file ('all-sql.log' in this example). This provides a convenient way to troubleshoot database-related issues or perform auditing.
The above is the detailed content of How to Log SQL Queries Comprehensively in a Django Application?. For more information, please follow other related articles on the PHP Chinese website!