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=Trueonly 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 flag | Value / default | Applies to | Source |
|---|---|---|---|
handler filters | A list of filter IDs for this handler, e.g. ['redact']; ID is application-defined | Python current docs | schema |
filters.<id>.() | Importable factory for your sanitizer | Python current docs | custom objects |
defaults | A dictionary with default values to use in custom fields, e.g. {'request_id': '-'}; formatter default None | Python current docs | schema, Formatter |
disable_existing_loggers | Set False if existing library loggers should remain enabled; default True | Python current docs | schema |
incremental | False when installing filters/formatters; True ignores both sections | Python current docs | schema |
LoggerAdapter | Contextual information mapping; default adapter handling can overwrite per-call extra | Python current docs | cookbook |
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) | Cause | Fix | Source |
|---|---|---|---|
Formatting field not found in record: | Formatter field missing from record values and defaults | Configure safe defaults and cover unrelated logger records | source, defaults proposal |
Invalid format | Formatter pattern and style disagree | Retain validation | source, 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