| id | setup-structured-logging |
|---|---|
| type | playbook |
| title | Set Up Production Structured Logging |
| summary | Configure structured JSON logging with context propagation, sensitive data redaction, and environment isolation. |
| lang | en-US |
| content_version | 1 |
| status | reviewed |
| reviewed_on | 2026-09-02 |
- Choose JSON-formatted structured output for production and colorized readable
text for local development; avoid ad-hoc
print()and unstructured string concatenation. - Propagate a unique
request_idortrace_idthrough every incoming HTTP request, background job, and downstream service call using Pythoncontextvars. - Standardize log event schemas: include timestamp (ISO 8601), log level, logger name, event name, trace identifiers, and structured payload fields.
- Implement automatic redaction filters for sensitive fields (e.g.
password,token,authorization,api_key, and personally identifiable information). - Capture full exception stack traces with
exc_info=Trueonly at service boundaries; do not log redundant raw tracebacks across multiple nested catch blocks. - Verify log generation under test by asserting structured dictionary fields rather than brittle substring matches.