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 the matched path(s). Defaults to .*
  • SQLLOG_LOG the file to write out the logs to. Defaults to django_queries.jsonl
  • SQLLOG_LOG_POST log request.POST data. Defaults to False
  • SQLLOG_LOG_META log request.META data. Defaults to False

Any outputs that can't be JSON serialized will be rendered as <not serializable>

Download files

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

Source Distribution

django-sql-log-middleware-0.1.8.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

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

django_sql_log_middleware-0.1.8-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file django-sql-log-middleware-0.1.8.tar.gz.

File metadata

File hashes

Hashes for django-sql-log-middleware-0.1.8.tar.gz
Algorithm Hash digest
SHA256 0acaed1027e1392fe7bca998dd2b922e432c060fac62ec5b48a5f0a50510e578
MD5 4f2a9348e91ba2e76da4f18c0f6eb2c4
BLAKE2b-256 7640b51f2ccbcb1415a755031871ccbe30da6fdef210927c4a542a8553ee2894

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_sql_log_middleware-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 95a05c1243b6e28caf1941b32c37ee1187743f190f0ed159ad4c2cc59ea6c9b1
MD5 51019c55771f5ff83171527e63471450
BLAKE2b-256 7687841571c0d4df261ca3a6a47cb0bb76edaef56cdc80c7d01484985748363d

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.8 This release

2 files

0.1.7

1 file

0.1.6

1 file

0.1.5

1 file

0.1.4

1 file

0.1.3

1 file

0.1.2

1 file

0.1.1

2 files

Supported by

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