Back to Blog
Want to Learn

Contextual Logging

A field story in three parts about building a contextual logger for Node (the library now called spawntrail). Part 1: the Mapped Diagnostic Context idea, a naive singleton context, and the concurrency bug that mislabels production logs the first time two requests overlap.

3 articles
~25 min total
Node.jsLoggingBackendAsyncLocalStorageObservabilityConcurrency

Articles in this Series

AsyncLocalStorageBackendConcurrencyLoggingNode.jsObservabilityTypeScriptasync-hookspinowinston
1
Want to Learn
Contextual Logging, Part 1: The Singleton Trap
A field story in three parts about building a contextual logger for Node (the library now called spawntrail). Part 1: the Mapped Diagnostic Context idea, a naive singleton context, and the concurrency bug that mislabels production logs the first time two requests overlap.
Jul 16, 20268 min read
Node.jsLoggingObservabilityConcurrency+1
2
Want to Learn
Contextual Logging, Part 2: AsyncLocalStorage to the Rescue
Part 2 of building spawntrail: replacing the singleton with Node's AsyncLocalStorage so each request carries its own context across every await, proving isolation with an interleaved-concurrency test, and the new problems the rewrite quietly introduced.
Jul 17, 20268 min read
Node.jsAsyncLocalStorageLoggingasync-hooks+1
3
Want to Learn
Contextual Logging, Part 3: Injecting at Log Time
Part 3, the version on npm as spawntrail: inject context at log time through the logger's own hook (winston format, pino mixin) so mid-request context appears and the tool stays logger- and framework-agnostic. The MDC rebuilt on AsyncLocalStorage, the honest alternatives, and the lineage it supersedes.
Jul 18, 20269 min read
Node.jsAsyncLocalStoragewinstonpino+1