📝 clarify logger bind alias contract

This commit is contained in:
Nanaloveyuki
2026-06-14 11:37:35 +08:00
parent c800042e46
commit 1124868bd8
+10 -3
View File
@@ -28,15 +28,18 @@ pub fn[S] Logger::bind(self : Logger[S], fields : Array[Field]) -> Logger[Contex
#### output
- `Logger[ContextSink[S]]` - New logger wrapper that prepends shared fields at write time.
- `Logger[ContextSink[S]]` - New logger value whose visible sink type becomes `ContextSink[S]` and prepends shared fields at write time.
### Explanation
Detailed rules explaining key parameters and behaviors
- `bind(...)` delegates directly to `with_context_fields(...)`.
- The original logger value is not mutated; a wrapped logger is returned.
- `bind(...)` delegates directly to `with_context_fields(...)`; it is an alias, not a different implementation.
- The original logger value is not mutated; a derived wrapped logger is returned.
- The returned logger changes visible type from `Logger[S]` to `Logger[ContextSink[S]]` because the sink pipeline is extended with stored shared fields.
- Stored target, minimum level, and timestamp behavior are preserved while the sink pipeline changes.
- Shared fields are applied to every later log call emitted through the returned logger.
- When a log call also passes per-record fields, the shared bound fields are prepended before those per-call fields because `bind(...)` inherits the same merge order as `with_context_fields(...)`.
- This alias is useful when you prefer shorter chaining syntax in application code.
### How to Use
@@ -64,6 +67,8 @@ let worker = Logger::new(console_sink(), target="app")
In this example, `bind(...)` communicates intent without changing underlying behavior.
And the returned logger still keeps the same target and level-gating behavior as the source logger.
### Error Case
e.g.:
@@ -77,3 +82,5 @@ e.g.:
2. This alias exists for ergonomics, not for different semantics.
3. Use `with_context_fields(...)` when the longer name makes the sink-type change to `ContextSink[S]` clearer at the call site.