Context Propagation in Logs
You'll learn how to track a single request or operation across multiple log statements by attaching shared context like correlation IDs.
When a user request travels through your system—hitting multiple services, functions, or threads—you end up with dozens or hundreds of log entries. Without context propagation, these logs are like puzzle pieces scattered across different boxes: you know something happened, but you can't reconstruct the story.
Context propagation means attaching identifying information (typically a correlation ID or request ID) to every log statement related to the same logical operation. Think of it like giving every passenger on a train the same ticket number—later, you can gather all logs with that ticket number to see the complete journey.
How It Works
When a request enters your system, generate or extract a unique identifier. Then, ensure every log statement emitted while handling that request includes this ID as a structured field:
{"level":"info","correlationId":"abc-123","message":"Request received"}
{"level":"debug","correlationId":"abc-123","service":"auth","message":"Validating token"}
{"level":"info","correlationId":"abc-123","service":"database","message":"Query executed"}
Now you can filter your centralized logs by correlationId: "abc-123" and see the complete request flow, even across microservices.
Context Beyond IDs
Beyond correlation IDs, propagate other useful context: user ID, tenant ID, session ID, trace ID (for distributed tracing), or operation type. This enriches every log entry automatically without manual repetition.
Many logging libraries support contextual loggers that carry this metadata through the entire request lifecycle, automatically appending it to every log statement.
Key Takeaway:
Context propagation—especially correlation IDs—transforms scattered log entries into traceable, queryable stories of how requests flow through your system.