디버깅 및 성능 분석을 위해 Django에서 모든 SQL 쿼리를 캡처하는 방법은 무엇입니까?

Patricia Arquette
풀어 주다: 2024-10-17 17:28:30
원래의
417명이 탐색했습니다.

How to Capture All SQL Queries in Django for Debugging and Performance Analysis?

Logging All SQL Queries in Django

In Django, capturing the SQL queries executed by your application can provide invaluable insights for debugging, performance analysis, and security monitoring. Here's how you can accomplish this:

To log all SQL queries, including those from the admin site, modify the LOGGING configuration in your settings.py file. Add the following snippet to merge with the existing LOGGING field:

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'],
        }
    }
}
로그인 후 복사

This configuration will route all SQL queries to the console when the DEBUG flag is set to True in your settings.py. If you prefer to log the queries to a file, you can replace the 'console' handler with a 'FileHandler' and specify the path to the log file.

For example, to log the queries to a file named all-sql.log, use the following handler:

'handlers': {
    'all_sql_file': {
        'level': 'DEBUG',
        'filters': ['require_debug_true'],
        'class': 'logging.FileHandler',
        'filename': 'all-sql.log',
    }
}
로그인 후 복사

Make sure to restart your Django server after making these changes to ensure that the new logging configuration takes effect.

위 내용은 디버깅 및 성능 분석을 위해 Django에서 모든 SQL 쿼리를 캡처하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
저자별 최신 기사
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!