Structlog

Django-log-outgoing-requests is structlog friendly and ships a built-in processor for request/response metadata and bodies.

Log events

The library emits two kinds of log events, with a format that’s structlog-friendly:

outgoing_request_response_received

Emitted whenever a successful request-response cycle is completed.

Note

When requests follows redirects, this leads to a log event for each request-response cycle in the redirect chain being logged.

outgoing_request_errored

Emitted whenever an outgoing request failed. Usually there’s no response information available.

Processors

Log-outgoing-requests includes a built-in structlog processor that extracts the request and response details and adds them to the event dict. Typically, you’ll want to include this in your loggers foreign pre chain, for example:

 1LOGGING = {
 2    "version": 1,
 3    "disable_existing_loggers": False,
 4    "formatters": {
 5        # structlog - foreign_pre_chain handles logs coming from stdlib logging module,
 6        # while the `structlog.configure` call handles everything coming from structlog.
 7        # They are mutually exclusive.
 8        "json": {
 9            "()": structlog.stdlib.ProcessorFormatter,
10            "processor": structlog.processors.JSONRenderer(),
11            "foreign_pre_chain": [
12                structlog.contextvars.merge_contextvars,
13                structlog.processors.TimeStamper(fmt="iso"),
14                structlog.stdlib.add_logger_name,
15                structlog.stdlib.add_log_level,
16                ExtractRequestAndResponseDetails(expand_headers=False),
17                structlog.stdlib.PositionalArgumentsFormatter(),
18            ],
19        },
20        "plain_console": {
21            "()": structlog.stdlib.ProcessorFormatter,
22            "processor": structlog.dev.ConsoleRenderer(),
23            "foreign_pre_chain": [
24                structlog.contextvars.merge_contextvars,
25                structlog.processors.TimeStamper(fmt="iso"),
26                structlog.stdlib.add_logger_name,
27                structlog.stdlib.add_log_level,
28                ExtractRequestAndResponseDetails(expand_headers=True),
29                structlog.stdlib.PositionalArgumentsFormatter(),
30            ],
31        },
32    },
33    ...
34}

API reference

class log_outgoing_requests.structlog.ExtractRequestAndResponseDetails(expand_headers: bool = False, extract_bodies: bool = False, body_max_content_length: int = 10240)

Structlog processor that can extract request and response details from log records.

Use this processor in your foreign pre-chain configuration to extract request, response and error details from outgoing request logs.

Parameters:
  • expand_headers – Boolean that controls whether the request and response headers are added as a container or individual (prefixed) keys to the event dict. For the JSON renderer, you typically want to leave this disabled, while for the console renderer (logfmt) you’ll probably want to set it to True.

  • extract_bodies

    If enabled, request and response bodies smaller than the treshold (see body_max_content_length) are added to the event dict as well. Additionally, the LOG_OUTGOING_REQUESTS_CONTENT_TYPES setting is checked for an allow-list of content types to log. Be careful when you enable this, as it can quickly explode your log storage.

    Note that bodies from streaming responses (e.g. requests.get(url, stream=True)) are never extracted.

  • body_max_content_length – If body extraction is enabled, this parameter controls the maximum size of bodies to be logged. Bodies that are larger will not be added to the event dict.