Module kv

Module kv 

Source
Expand description

Structured logging.

Structured logging enhances traditional text-based log records with user-defined attributes. Structured logs can be analyzed using a variety of data processing techniques, without needing to find and parse attributes from unstructured text first.

Keys are strings and Values are a datum of any type that can be formatted or serialized. Simple types like strings, booleans, and numbers are supported, as well as arbitrarily complex structures involving nested objects and sequences.

Value uses value-bag crate as the backend, which is an alias of value_bag::ValueBag.

KVs will be passed into a Record to be processed by Formatters via Record::key_values method.

§Examples

§Basic syntax

In logging macros, an optional named parameter kv (like logger) is used to add key-values to a log.

info!("program started", kv: { pid = std::process::id() });

trace!(logger: telemetry, "user logged in", kv: { username = "John" });

let ip = "1.1.1.1";
trace!("DNS setup", kv: { ip });
//                        ^^ Shorthand syntax, equivalent to `ip = ip`
§Modifier

A value is stored directly with its type by default (after erasure, of course), using modifier if you want it to be stored in another format.

ModifierDescription
No modifier, capture the value directly
:Capture the value using Display trait
:?Capture the value using Debug trait
:svalCapture the value using sval::Value trait, crate feature sval is required
:serdeCapture the value using serde::Serialize trait, crate feature serde is required
let url = Url::parse("https://example.com")?;
trace!("user browsed website", kv: { url: });
//                                      ^ Capture the value using `Display` trait
//                                   ^^^^ Shorthand syntax, equivalent to `url: = url`

let orders = vec!["coffee", "pizza", "soup"];
info!("order received", kv: { orders:? });
//                                  ^^ Capture the value using `Debug` trait
//                            ^^^^^^^^ Shorthand syntax, equivalent to `orders:? = orders`

#[derive(sval_derive::Value)]
struct Point { x: f32, y: f32 }

let pos = Point { x: 11.4, y: 5.14 };
trace!("user clicked", kv: { pos:sval });
//                              ^^^^^ Capture the value using `sval::Value` trait
//                           ^^^^^^^^ Shorthand syntax, equivalent to `pos:sval = pos`

Structs§

Key
Represents a key in a key-value pair.
KeyValues
Represents a collection of key-value pairs.
KeyValuesIter
Represents an iterator over key-value pairs.

Type Aliases§

Value
Represents a value in a key-value pair.