Print request and response headers, body (with pretty-printing), etc.
Project description
Django Debug Requests & Responses (DDRR)
Get more out of your runserver
development output! Print request and response
headers, body (with pretty-printing), etc. Highly customizable! Supports
Django 2.2+ and 3.0+, with Python 3.7+.
- Full request headers
- The entire request body
- Pretty-printing optional
- Colored output
- Super easy setup
- No extra dependencies
DDRR can also be used for general logging with some configuration of your own.
Installation
-
$ pip install ddrr
-
Add
"ddrr"
toINSTALLED_APPS
-
Insert
"ddrr.middleware.DebugRequestsResponses"
first inMIDDLEWARE
Done! When you run runserver
, you'll now get the entire HTTP requests and
responses, including headers and bodies.
If you don't like the default output format, read on...
Customization
DDRR = {
"ENABLE_REQUESTS": True, # enable request logging
"ENABLE_RESPONSES": True, # enable response logging
"LEVEL": "DEBUG", # ddrr log level
"PRETTY_PRINT": False, # pretty-print JSON and XML
"REQUEST_TEMPLATE_NAME": "ddrr/default-request.html", # request log template name
"REQUEST_TEMPLATE": None, # request log template string (overrides template name)
"RESPONSE_TEMPLATE_NAME": "ddrr/default-response.html", # response log template name
"RESPONSE_TEMPLATE": None, # response log template string (overrides template name)
"REQUEST_HANDLER": logging.StreamHandler(), # request log handler
"RESPONSE_HANDLER": logging.StreamHandler(), # response log handler
"ENABLE_COLORS": True, # enable colors if terminal supports it
"LIMIT_BODY": None, # limit request/response body output to X chars
"DISABLE_DJANGO_SERVER_LOG": False, # disable default django server log
}
Template contexts
If you want to customize request or response templates, you can use the following values:
- Request template context:
ddrr.body
- request bodyddrr.content_type
- request content typeddrr.formatter
- the formatterddrr.headers
- mapping of header fields and valuesddrr.method
- request methodddrr.path
- request pathddrr.query_params
- query parametersddrr.query_string
- query stringddrr.record
- the actual log record objectddrr.request
- the actual request object
- Response template context:
ddrr.content
- response contentddrr.content_type
- response content typeddrr.formatter
- the formatterddrr.headers
- mapping of header fields and valuesddrr.reason_phrase
- response reason phraseddrr.record
- the actual log record objectddrr.response
- the actual response objectddrr.status_code
- response status code
For example, this will log the method, path and body of each request, as well as the status code, reason phrase and content of each response:
DDRR = {
"REQUEST_TEMPLATE": "{{ ddrr.method }} {{ ddrr.path }}\n"
"{{ ddrr.body }}",
"RESPONSE_TEMPLATE": "{{ ddrr.status_code }} {{ ddrr.reason_phrase }}\n"
"{{ ddrr.content }}",
}
Pretty-printing
By default, pretty-printing is disabled. Set DDRR["PRETTY_PRINT"]
to True
to enable it.
Pretty-printing of JSON requires no external dependency.
Pretty-printing of XML uses minidom
by default and doesn't require any extra
dependency. If you want to use lxml
instead, which is slightly better at
pretty-printing XML, you can install that using pip install ddrr[xml]
.
How it works internally
The middleware ddrr.middleware.DebugRequestsResponses
sends the entire
request object as the message to ddrr-request-logger
. This logger has been
configured to use ddrr.formatters.DjangoTemplateRequestFormatter
which
internally uses Django's built-in template engine to format the request into
human-readable form. By default, this is shown in your console output, but you
can easily configure it to log it to a file, Logstash, or anything else.
Similar projects
Development and contributions
PR's are always welcome!
For hacking on DDRR, make sure you are familiar with:
Install dependencies and set up the pre-commit hooks.
$ poetry install
$ pre-commit install
The pre-commit hooks will, among other things, run Flake8 on the code base and
Black to make sure the code style is consistent across all files. Check out
.pre-commit-config.yaml
for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ddrr-3.1.0.tar.gz
.
File metadata
- Download URL: ddrr-3.1.0.tar.gz
- Upload date:
- Size: 11.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.9.12 Darwin/21.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60ed50efbb7dcae5d69949be43ef8c8f01491ae65d92010dbc665ea105ae3944 |
|
MD5 | 6519ea6e65cbff40aac2e66934328adc |
|
BLAKE2b-256 | 0ec47c0315be732ec389017af343c1a4e181d524ebb1ce49af53329cb8cd7763 |
File details
Details for the file ddrr-3.1.0-py3-none-any.whl
.
File metadata
- Download URL: ddrr-3.1.0-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.9.12 Darwin/21.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57809a43ced49ee0f1af7b635ef13df2e99bda1981a88f8d24595adb748cdca7 |
|
MD5 | 11f65b1d9ee90aacfc653d6620b2ad66 |
|
BLAKE2b-256 | 82c46d8bd80042e4c6832ca2884a73703a8cdfd5ed1e6621cfb8b8d1a2fbf3c6 |