Keep useful logs without copying secrets
Create deterministic JSON events with explicit, recursive redaction rules.
Logs are another output boundary
A useful event records what happened in a form another tool can parse. JSON Lines gives one JSON object per line, preserving field types and escaping embedded newlines correctly. A supplied event name and structured fields are easier to compare than a large interpolated sentence.
Do not copy arbitrary exception text, credentials or whole environment dictionaries into events. This exercise defines a small redaction policy: password, token, secret and authorization keys are replaced, ignoring case and surrounding whitespace. The traversal handles nested dictionaries and lists without changing the caller's original values.
A redactor is not a privacy guarantee
Key-based redaction only recognizes the documented keys. It cannot reliably discover a secret embedded in an unrelated message string or detect every organisation's sensitive field. Design the allowed event payload first, then use redaction as an additional boundary rather than collecting everything and hoping a filter will repair it.
The input is ordinary JSON-compatible data with string dictionary keys and no cycles. Preserve numbers, booleans and null values. Reject non-finite numbers when serializing, because NaN is not a portable JSON value and can break downstream consumers.
Sorted keys and compact separators make output reproducible. Unicode stays readable, while a final newline separates the next event. In the capstone, reports contain selected assessment fields; no terminal transcript, interactive input or user credentials are included automatically.
import json
event = {"event":"probe_complete","fields":{"address":"192.0.2.10","state":"open"}}
print(json.dumps(event, sort_keys=True, ensure_ascii=False))
Put it into practice.
Implement redact(value) for JSON-compatible data with string keys and no cycles. Recursively copy dictionaries/lists, replacing any password, token, secret or authorization key value with "[REDACTED]", matching strip().casefold() while preserving original key spelling. Other scalar values stay unchanged. Implement event_line(kind, fields): kind must match [a-z][a-z_]* and fields must be a dict, otherwise ValueError. Return UTF-8-compatible JSON text {event:kind, fields:redact(fields)} using sorted keys, compact separators, ensure_ascii=False, allow_nan=False and one final newline. Do not modify inputs.
See an explained solution
import json,re
def redact(value):
sensitive={"password","token","secret","authorization"}
if isinstance(value,dict):
return {key:"[REDACTED]" if key.strip().casefold() in sensitive else redact(item) for key,item in value.items()}
if isinstance(value,list):return [redact(item) for item in value]
return value
def event_line(kind, fields):
if not isinstance(kind,str) or not re.fullmatch(r"[a-z][a-z_]*",kind) or not isinstance(fields,dict):
raise ValueError("Invalid event")
return json.dumps({"event":kind,"fields":redact(fields)},sort_keys=True,ensure_ascii=False,separators=(",",":"),allow_nan=False)+"\n"
if __name__ == "__main__":
print(event_line("probe_complete", {"address":"192.0.2.10","token":"demo-secret","state":"open"}), end="")
The event writer emits selected structured data with a narrowly defined redaction policy. It preserves inputs and avoids implying that all sensitive text can be detected automatically.