"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Log All SQL Queries in Django?

How to Log All SQL Queries in Django?

Published on 2024-11-06
Browse:483

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:

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'],
        }
    }
}

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.

Release Statement This article is reprinted at: 1729157175 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3