Skip to main content

Django SQL Log Middleware

A middleware to write Django SQL queries and stats to a jsonl file. Makes analysis easier when you have log files that are hundreds of megabytes.

Inspired by the django-sqlprint-middleware project

Installation

pip install django-sql-log-middleware

Add it to the middleware section of settings.py:

MIDDLEWARE = [
    ...
    'django_sql_log_middleware.SqlLogMiddleware',
]

Output

Output will be written to the path specified in SQLLOG_LOG in the below format. Each line is a JSON object covering a single request. Time is the current unix time. Query time is measured in seconds.

{
        "path": "/path/to/api?abc=1234",
        "time": 1649301150,
        "queries": [
            {"sql": "select * from users", "time": 1.1234},
            {"sql": "select * from things", "time": 4.1234},
        ]
}

Settings

Specify the following settings in your settings.py file:

  • DEBUG This middleware will only run when DEBUG is set.
  • SQLLOG_MIDDLEWARE set this variable to False disable the middleware without removal. Defaults to True
  • SQLLOG_PATH_RE a regex pattern to only log SQL queries in hte matched path(s). Defaults to .*
  • SQLLOG_LOG the file to write out the logs to. Defaults to django_queries.jsonl

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_sql_log_middleware-0.1.4-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file django_sql_log_middleware-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for django_sql_log_middleware-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e4498ea43395a4263cb1cfb4cb0dfb7bded91fff57a4196c449b39e33726e8f3
MD5 7c427045f00b9f00148a7431575f9e2f
BLAKE2b-256 d882cf84bb06997b3587cca903cd739f7e3dced4780c836cd7823aa4f45b1139

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page