Skip to main content

Module context

Module context 

Source
Available on crate feature std only.
Expand description

Thread-local logging context.

Context carries ambient key-value data that gets attached to every record emitted from the current thread. Typical use: stash a trace_id / request_id at the entry of a request handler so all downstream log lines correlate. The ContextGuard returned by with_field (and friends) removes the value when dropped.

Context is intentionally thread-scoped. For async task propagation, capture context fields at task spawn and re-install them on the task’s executor thread; this crate does not depend on a specific async runtime.

Structs§

ContextGuard
RAII guard that removes a context slot when dropped.

Constants§

MAX_CONTEXT_SLOTS
Maximum number of context slots per thread. Fixed-size so the snapshot path is allocation-free.

Functions§

clear
Remove all context entries on the current thread.
len
Number of context entries on the current thread.
with_field
Push a key/value pair onto the current thread’s context.
with_request_id
Convenience: push a request_id field.
with_snapshot
Run f with the current thread’s context exposed as a borrowed slice of Field. No allocation: the slice is filled from a stack-resident buffer.
with_trace_id
Convenience: push a trace_id field.