Design a secrets-redacting structured logger
Design a structured logger that keeps credentials out of logs by default: key-name and value-pattern redaction, a Secret type that cannot be printed, URL and error scrubbing, bounds on depth, size and cycles, reserved fields that callers cannot overwrite, keyed fingerprints for correlation, and canary tests that prove it works.
01Understand the problem
- Combine key-based and pattern-based redaction, and know the failure modes of each
- Make secrets unprintable by type so redaction does not depend on reviewers
- Bound the logger against hostile input: depth, size, cycles, and reserved fields
- Correlate redacted values with keyed fingerprints, and prove the logger works with canaries
What they probe: where redaction happens (before serialisation, at the sink), how you catch a token logged under an innocent key, what happens with a circular object or a 50 MB string, whether a caller can overwrite level, and how you would know it is working in production. Where candidates slip: a regex over the final JSON string, redaction only by exact key names, SHA-256 of a secret as a 'safe' fingerprint, and exceptions whose messages contain connection strings.
Secrets reach logs through ordinary code: log.info('calling api', { headers }), an exception message with a connection string, a URL with ?token=, a debug dump of a request object. Logs then travel to aggregators, vendors, laptops and backups with broad read access. A redacting logger makes the safe outcome the default: the call site does nothing special, and the logger removes what should never be stored.
- Fields whose key names suggest a secret (
password,authorization,apiKey,set-cookie) are replaced with[REDACTED]. - String values anywhere are scanned for credential shapes (JWTs, AWS access key ids, GitHub tokens, PEM private keys, bearer headers, credentials in URLs).
- Values wrapped in a
Secrettype are always redacted. - Errors are logged as name, scrubbed message and optional scrubbed stack.
- Depth, keys per object, array length and string length are capped; cycles are detected.
- Reserved fields (
ts,level,msg) cannot be overwritten by caller data.
| Technique | Catches | Misses |
|---|---|---|
| Key-name denylist | { password: 'x' }, { 'X-Api-Key': 'y' } | A token under { value: ... } or inside free text |
| Value patterns | Known formats anywhere: eyJ..., AKIA..., ghp_..., PEM blocks | Random secrets with no recognisable shape |
| Secret type | Anything the code already knows is secret | Secrets that arrive as plain strings from elsewhere |
| Field allow-list (schema) | Everything not explicitly permitted | Nothing, but needs a schema per event type |
This is an advanced module
Problems: policy and crypto engines is part of the Pro pass. 4 more sections — the deep dives, the final design, follow-ups and the test — unlock with it.
Already a cyart.io or studio.cyart.io member? Your pass code is in your Studio account.