Skip to main content

Write Django's SQL statements to a JSONL log file

Project description

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_LOGFILE 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

Use the following settings:

  • 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

Project details


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.1.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

File details

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

File metadata

File hashes

Hashes for django-sql-log-middleware-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f700ed196aee2867537ddd92a246df786ffc8a60c9fdce9f97d03e635c717106
MD5 845f60b9b8bd37b6e4f751de52fe97aa
BLAKE2b-256 e57e807bcac16e4f14292ecb4375ad2fac844acc280988b0ab81623c91ba522f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_sql_log_middleware-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 971d5a67ba3a68de85153c7405abe0ce4ae1b9fbb6f140bc323d0a6551e679e5
MD5 ac088c36c21c85ce556038541ed4868a
BLAKE2b-256 7cbc37b059344fb97765a3c8c6c331c975ab2c783ca7124141376a6461551ca4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page