Skip to content

logger: add pre-print callback hook + PII redaction filter#328

Merged
paulcruse3 merged 5 commits into
masterfrom
sweet-12-logger-redaction-callbacks
Jun 17, 2026
Merged

logger: add pre-print callback hook + PII redaction filter#328
paulcruse3 merged 5 commits into
masterfrom
sweet-12-logger-redaction-callbacks

Conversation

@paulcruse3

@paulcruse3 paulcruse3 commented Jun 16, 2026

Copy link
Copy Markdown
Member

What

Adds two things to acai_aws.common.logger:

  1. A generic pre-print callback hook on CommonLogger. register_callback(callback) / reset_callbacks(). Every log record ({level, time, trace, log}) is passed through the registered callbacks before it is printed; each returns the record to emit. Extension point for redaction, enrichment, or routing.

  2. A RedactionFilter class built on that hook. RedactionFilter(keys=[...], patterns=[...], redact_with='[REDACTED]'); the instance is callable, so it registers directly as a callback. Redacts matching field names (case-insensitive, any depth) and regex matches in string values. redact_with='' or any string overrides the replacement.

Opt-in by design. Nothing is registered automatically and there is no default key/pattern list. A caller wires the filter they want:

from acai_aws.common.logger.common_logger import CommonLogger
from acai_aws.common.logger.redaction import RedactionFilter

CommonLogger.register_callback(RedactionFilter(keys=['ssn', 'account_number'], patterns=[r'\b\d{3}-\d{2}-\d{4}\b']))

Notes

  • Backward compatible: record shape (level/time/trace/log) and JSON/PRETTY/INLINE output unchanged; only matching fields/values are rewritten, and only when a filter is registered.
  • Callback contract: callback(record: dict) -> dict. Runs in registration order.
  • Fail-safe: if a callback raises, the existing module-level log() try/except drops that line rather than emitting it.
  • Docs: README gains a Logging & Redaction section; .agents/AGENTS.md logger guidance extended.
  • Version: setuptools_scm derives version from tags, so this warrants a minor bump on release (additive feature).

Validation

  • pytest tests/acai_aws: 609 passed (8 new: RedactionFilter unit tests + callback integration tests).
  • pylint acai_aws --recursive=y --fail-under 10: 10.00/10.

Part of SWEET-12.

Add an extensible callback mechanism to CommonLogger: register_callback /
reset_callbacks run each log record through registered callables before it
is printed (record = {level, time, trace, log}). The hook is generic
(redaction, enrichment, routing).

Add redaction_filter(keys, patterns, redact_with='[REDACTED]') built on
that hook: redacts matching field names (case-insensitive, any depth) and
regex value matches. PII redaction with sensible defaults is registered by
default so logs are scrubbed before they reach stdout/CloudWatch; opt out
with ACAI_LOG_REDACTION=off.
Replace the module-level redaction_filter function and the defaults
module (module-level functions + vars) with a single RedactionFilter
class: keys/patterns/redact_with via **kwargs, callable so an instance
is the callback, defaults as class constants, register_default classmethod
for the env-gated wiring. Drop descriptive what-docstrings from
CommonLogger.
Add a Logging & Redaction section to the README (callback hook + custom
filter usage, default-on PII redaction, ACAI_LOG_REDACTION opt-out) and
extend the .agents AGENTS.md logger guidance with the same.
Remove the auto-registered default PII filter, the DEFAULT_KEYS /
DEFAULT_PATTERNS lists, the register_default classmethod, and the
ACAI_LOG_REDACTION env switch. RedactionFilter is now purely opt-in:
callers register it with their own keys/patterns. Docs updated to match.
@sonarqubecloud

Copy link
Copy Markdown

@paulcruse3 paulcruse3 merged commit 791b5be into master Jun 17, 2026
9 checks passed
@paulcruse3 paulcruse3 deleted the sweet-12-logger-redaction-callbacks branch June 17, 2026 02:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant