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

Linda Hamilton
發布: 2024-10-17 17:27:30
原創
164 人瀏覽過

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中文網其他相關文章!

來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!