Skip to content

LogContext

const LogContext: object

Defined in: src/LogContext.ts:59

The LogContext namespace exposes the MDC operations. The class- style LogContext.run(...) shape mirrors how Akka’s MDC is used and keeps the public API tight without exporting the underlying AsyncLocalStorage instance.

get(): LogContextData

Read the current context. Returns the frozen empty object when called outside any run — never undefined, never null, so callers can .entries() over it without guarding.

LogContextData

run<T>(ctx, fn): T

Run fn with ctx as the current context. The previous context (if any) is shadowed for the duration of the call and restored automatically. Sync and async fn both work — AsyncLocalStorage preserves the binding across awaits.

T

LogContextData

() => T

T

snapshot(): Record<string, string | number | boolean>

Capture the current context as a plain (mutable-by-the-caller) object — useful when you need to pass the context through a boundary that strips Readonly (e.g. a JSON serialiser). Returns a fresh copy every call.

Record<string, string | number | boolean>

with<T>(extra, fn): T

Run fn with extra fields merged into the current context. Equivalent to run({ ...get(), ...extra }, fn) but a touch shorter at call sites that just want to add a field.

T

LogContextData

() => T

T