首頁  >  文章  >  後端開發  >  如何在 Django 中記錄所有 SQL 查詢?

如何在 Django 中記錄所有 SQL 查詢?

Linda Hamilton
Linda Hamilton原創
2024-10-17 17:27:30166瀏覽

How to Log All SQL Queries in Django?

How to Log SQL Queries in Django

Logging all SQL queries executed by a Django application can be beneficial for debugging and performance analysis. This article provides a step-by-step guide on how to achieve this effectively.

Configuration

To log all SQL queries, including those generated by the admin site, integrate the following snippet into the LOGGING field within your settings.py file:

<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>

Results

Upon implementation, all SQL queries performed by your Django application will be recorded in the specified log file, providing a comprehensive record of database interactions for troubleshooting and analysis.

以上是如何在 Django 中記錄所有 SQL 查詢?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn