log_redaction
Logging filter that redacts API keys in log output.
Attaches to the root logger so all loggers (including httpx/httpcore)
have keys partially censored: first 4 chars + ... + last 4 chars.
- log_redaction.redact_api_keys(text)[source]
Replace API key values in text with a partially-masked form.
Scans the input for
key=<secret>patterns via_KEY_REand rewrites each one through_redact_match()so the secret survives in logs only as its first four and last four characters joined by an ellipsis. This keeps log lines correlatable without ever exposing a full credential. Pure string transformation with no I/O or side effects.Called by
ApiKeyRedactionFilter.filter()for every log record routed through the handler the filter is attached to, and exercised directly bytests/core/test_enhanced_logging.py.
- class log_redaction.ApiKeyRedactionFilter(name='')[source]
Bases:
FilterLogging filter that censors API keys before they reach the handler.
A
logging.Filtersubclass that sanitizes rather than suppresses: every record passing through is allowed onward, but any embeddedkey=secret is partially masked first viaredact_api_keys(). Installing it on the root/console handler means downstream-library noise (httpx, httpcore) is scrubbed alongside the bot’s own log lines.Instantiated and attached to a handler by
core/log_config.py(which callsaddFilteron the console handler), and asserted to be present bytests/core/test_enhanced_logging.py. The actual per-record work lives infilter().- filter(record)[source]
Redact API keys in a log record in place before it is emitted.
Implements the
logging.Filtercontract. Renders the record’s fully-formatted message viarecord.getMessage()(which interpolatesrecord.args), passes it throughredact_api_keys(), and — only when redaction actually changed the text — overwritesrecord.msgwith the censored string and clearsrecord.argstoNoneso the handler does not attempt a second%-format pass against the already-substituted message. The record is always allowed through (returnsTrue); this filter sanitizes rather than suppresses.Registered on a handler (typically the root/console handler) by
core/log_config.py, which constructs anApiKeyRedactionFilterand callsch.addFilter(...); the standard logging machinery invokes this method for every record routed through that handler. It is also asserted to be present bytests/core/test_enhanced_logging.py.