2018-08-30 12:20:00+00:00

Introduction

Database access is the main bottleneck of a web app.

Many infrastructure related efforts are done to improve database access, such as using in memory database or replacing storage from hard disk to SDD.

My Programmer's mistakes when coding database access also greatly contribute to the severity level of this bottleneck problem.

Logging the executed database SQL is a great tool to help checking if our code must be improved or not.

Enabling SQL Logging in Django 2

Having these setting values is enough to make SQL executions to get logged.

Set DJANGO_LOG_LEVEL environment variable:

$ export DJANGO_LOG_LEVEL=DEBUG

Set django settings values:

import os

DEBUG = True
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
        },
    },
}

Restart the app server.