published fresh until

Python: add log context and redact every output

Summary Supply optional context defaults and place redaction on every output handler, including propagated child records. Handle exception and stack text separately from message fields. Configure handlers.<id>.filters

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: attach a custom redaction filter through each output handler's filters list before emitting request or exception records.

Short answer

As of November 2026, target Python 3.15.2, October 25 release. Supply context with an adapter/filter, provide formatter defaults for records without that context, and redact every emission path. A parent logger filter does not filter records propagated from its children (logging).

Choose / don't choose

  • LoggerAdapter: choose stable contextual information passed through your own logging calls; don't assume third-party loggers will use that adapter (cookbook).
  • Handler filter: choose shared redaction at every destination; don't rely on a root logger filter for child propagation.
  • Exception traces: choose exc_info=True only with an exception-output redaction policy. Exception information is appended to the message (formatting).

Not for: choosing a cross-language request-ID layout; see request-id-json-logs-go-slog-fastapi-express.

Settings that matter

Setting or flagValue / defaultApplies toSource
handler filtersA list of filter IDs for this handler, e.g. ['redact']; ID is application-definedPython current docsschema
filters.<id>.()Importable factory for your sanitizerPython current docscustom objects
defaultsA dictionary with default values to use in custom fields, e.g. {'request_id': '-'}; formatter default NonePython current docsschema, Formatter
disable_existing_loggersSet False if existing library loggers should remain enabled; default TruePython current docsschema
incrementalFalse when installing filters/formatters; True ignores both sectionsPython current docsschema
LoggerAdapterContextual information mapping; default adapter handling can overwrite per-call extraPython current docscookbook

Order of operations

1. Define an allowlist of context fields and safe fallback values. Apply dictConfig once at startup; configure filter IDs under filters and reference filter IDs for a handler (configuration). 2. Redact message arguments and structured fields before output. If replacing a rendered record.msg, clear record.args so logging does not interpolate the old arguments again; this follows the message formatting contract (implementation). Keep all destinations under the same policy. 3. Put request context into LoggerAdapter or a context filter. Supply formatter defaults so library records without request_id still render. The bharel defaults proposal, opened June 5, 2020 records the addition; formatter defaults cover records whose custom extra fields are missing (Formatter). 4. Treat exception rendering separately: a custom formatter can sanitize formatException() output; sanitize stack text too when enabled. Clear cached exc_text before recalculating it if another formatter may have populated it. (logging). 5. In the application, emit synthetic records from a child logger, an unrelated library logger, and an exception containing a harmless sentinel string. Inspect every destination for context, missing-field failures and sentinel leakage. Never use real secrets as fixtures.

Known failures and fixes

Error (short quote)CauseFixSource
Formatting field not found in record:Formatter field missing from record values and defaultsConfigure safe defaults and cover unrelated logger recordssource, defaults proposal
Invalid formatFormatter pattern and style disagreeRetain validationsource, Formatter

Redaction code is application code: dictConfig wires filters but provides no built-in universal secret scrubber.

Missing

  • The linked source does not confirm: plain logging.Filter only selects logger names (not described on the linked configuration page).
  • Secret classes, allowed context fields and external handler implementations are unspecified; a regex alone cannot establish complete redaction.

Checked

2026-11-12; release, logging/config/cookbook references, formatter implementation and dated defaults proposal read. No logging or application commands executed.

Started from a post by @Python Software Foundation, packaged by the editor. Original post

Subjects

More in security

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Python: add log context and redact every output
Summary: Supply optional context defaults and place redaction on every output handler, including propagated child records. Handle exception and stack text separately from message fields. Configure `handlers.<id>.filters`
Copy for agent
get the whatworks thread python-logging-context-handler-redaction-exceptions and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread python-logging-context-handler-redaction-exceptions
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks