1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//! Tower middleware support for wide-log.
//!
//! **Feature gate:** `tokio`.
//!
//! The `WideLogLayer` middleware is generated by the [`wide_log!`](crate::wide_log!)
//! macro alongside the other generated items. It wraps every incoming request
//! in a `scope_default()` call, creating a wide-log guard and task-local storage
//! automatically — no manual `scope_default()` or `WideLogGuard::new()` needed
//! in handler code.
//!
//! ## Usage with axum
//!
//! ```rust,ignore
//! let app = Router::new()
//! .route("/ok", get(ok))
//! .layer(WideLogLayer);
//! ```
//!
//! ## Why middleware, not `WideLogGuard::new()`
//!
//! `tokio::task_local!` has no imperative setter — you can only set a
//! task-local value by wrapping a future with `.scope(value, future)`. The
//! middleware provides that wrapper automatically. `WideLogGuard::new()`
//! (the sync API) sets `thread_local!`, which is stale if a multi-threaded
//! runtime moves the task to another thread.
//!
//! ## No special setup in nested calls
//!
//! Any `info!()`, `warn!()`, `wl_set!`, etc. call — whether in the handler
//! directly, in a called sync function, or in a called async function — finds
//! the event via `current()`, which checks task-local first. No arguments or
//! threading needed.
//!
//! ## Generated types
//!
//! - `WideLogLayer` — implements `tower::Layer`; `Clone`
//! - `WideLogMiddleware<S>` — implements `tower::Service`; `Clone` when `S: Clone`
//! - `WideLogFuture<ResBody, Err>` — the response future, wraps the inner
//! future in `scope_default()`