JSON Logger (Python)
item / server / observability
JSON-lines logger with key-suffix secret redaction. Installs a formatter on the root logger so third-party logging comes out as JSON too.
- type
- item
- files
- 2
- registry deps
- none
- npm deps
- none
- env vars
- LOG_LEVEL
installs
- ~/backend/log_json.py
- ~/backend/tests/test_log_json.py
- Installs a JSON formatter on the root logger, so third-party logging comes out as JSON too
- Keys ending in key, secret, token, password, authorization or cookie are redacted before serialisation, recursively
- child(run_id=...) bakes fields into every line; LOG_LEVEL is read once at configure()
Call once at startup: log = log_json.configure(service="acme-api"), then log.info("job done", run_id=run_id); log.child(run_id=...) bakes fields into every line. One JSON object per line: ts, level, service, msg, your fields, then err when log.exception() attaches one. Any field whose key ends in key, secret, token, password, authorization or cookie (or equals auth) is replaced with [redacted] before serialisation, recursively — extend REDACT_SUFFIXES for app-specific names. LOG_LEVEL is read when configure() runs. Values that cannot be serialised fall back to str(); a logger must never crash the app.
Variations
one state per section
No previews for this item yet.
