mirror of
https://github.com/Nanaloveyuki/BitLogger.git
synced 2026-05-30 15:42:25 +00:00
1.9 KiB
1.9 KiB
name, group, category, update-time, description, key-word
| name | group | category | update-time | description | key-word | ||||
|---|---|---|---|---|---|---|---|---|---|
| fields | api | record | 20260512 | Build structured field arrays ergonomically from key-value tuples. |
|
Fields
Create an Array[Field] from an array of (String, String) tuples. This helper reduces repetitive field(...) calls when binding context or constructing event fields.
Interface
pub fn fields(entries : Array[(String, String)]) -> Array[Field] {}
input
entries : Array[(String, String)]- Tuple entries where.0is the field key and.1is the field value.
output
Array[Field]- Structured field array ready for logger APIs.
Explanation
Detailed rules explaining key parameters and behaviors
- Each tuple becomes one
Fieldvalue. - Order is preserved.
- This helper is purely ergonomic and does not add validation beyond normal string handling.
- It is commonly used with
bind(...),with_context_fields(...), and per-event field arguments.
How to Use
Here are some specific examples provided.
When Build Reusable Context Fields
When binding stable metadata to a logger:
let logger = Logger::new(console_sink(), target="audit")
.bind(fields([("service", "billing"), ("scope", "login")]))
In this example, tuple syntax is shorter and easier to scan than repeated field(...) calls.
When Build Small Per-event Field Arrays
When logging a few fields inline:
logger.info("accepted", fields=fields([("user", "alice"), ("status", "ok")]))
In this example, the helper keeps call sites compact.
Error Case
e.g.:
-
If
entriesis empty, the result is just an empty field array. -
Duplicate keys are preserved rather than collapsed.
Notes
-
Use
field(...)when only one field is needed. -
Use
fields(...)when tuple syntax improves readability.