context-logger
A small structured context propagation layer for the standard Rust log
ecosystem.
context-logger keeps your existing log::info!, log::warn!, etc. calls
unchanged and adds scoped structured context on top — inherited request-level
fields, local operation fields, computed defaults, and async-safe propagation.
Why context-logger?
The standard log crate is a small and widely used logging facade, but it does
not provide scoped context propagation by itself. This becomes noisy when every
log line should carry fields like request_id, user_id, tenant_id, or
operation.
context-logger fills this gap by adding a small runtime context layer on top
of ordinary log calls:
- Works with existing
logcalls — keep usinglog::info!,log::warn!, and your existing logger backend. - Fully dynamic context — create, extend, merge, and pass context as ordinary runtime data. Add fields at any point in your code without declaring spans.
- Scoped structured fields — attach request-level fields, operation-level fields, and default fields to every log record, with explicit control over what is inherited by child scopes.
- Async propagation — carry context through
.awaitwithFutureExt.
Is this a replacement for tracing?
No.
context-loggeris focused on logs, not traces.
tracing puts logs and spans into one instrumentation model.
context-logger keeps logs as regular log records and adds scoped structured
context to them. Context remains ordinary runtime data, so existing
log::info!, log::warn!, etc. calls stay unchanged.
What about traces?
Use a dedicated tracing library such as fastrace.
This keeps the two concerns separate:
context-loggerenriches ordinarylogrecords with scoped structured context;fastracerecords timeline spans and exports traces through OpenTelemetry.
Together they cover structured logs and distributed traces without requiring all
logging to go through the tracing instrumentation model.
Example output
|
{
}
{
}
{
}
Usage
Basic Example
Add context-logger to your Cargo.toml:
[]
= "0.4"
= "0.2"
= { = "0.4", = ["kv_serde"] }
= { = "0.11", = ["kv"] }
Then, you can use it in your code:
use ;
use info;
Async Context Propagation
Context logger supports async functions and can propagate log context across
.await points.
use ;
use info;
async
async
async
License
This project is licensed under the MIT License. See the LICENSE file for details.