pub struct LogFields(/* private fields */);Expand description
A set of key-value fields that can be attached to a logging scope.
LogFields stores structured attributes that extend
the key-values source of a log::Record. When a record flows through
crate::ContextLogger, these fields are merged into the record’s own
log::kv::Source so they appear in the output alongside the
user-provided key-values.
§Ordering
The order in which fields appear is not guaranteed. Do not rely on any specific ordering of keys.
Implementations§
Source§impl LogFields
impl LogFields
Sourcepub fn with(
self,
key: impl Into<Cow<'static, str>>,
value: impl Into<LogValue>,
) -> Self
pub fn with( self, key: impl Into<Cow<'static, str>>, value: impl Into<LogValue>, ) -> Self
Inserts a key-value pair into this collection, returning the collection for chained calls.
This method takes ownership of self, so it can be used as part of a
builder-style chain:
§Examples
use context_logger::LogFields;
let fields = LogFields::new()
.with("user_id", "user-123")
.with("request_id", 42);Sourcepub fn insert(
&mut self,
key: impl Into<Cow<'static, str>>,
value: impl Into<LogValue>,
) -> &mut Self
pub fn insert( &mut self, key: impl Into<Cow<'static, str>>, value: impl Into<LogValue>, ) -> &mut Self
Inserts a key-value pair into this collection.
Unlike Self::with, this method borrows self and
returns a mutable reference, allowing it to be used when chaining with
other methods that require borrowing.
§Examples
use context_logger::LogFields;
let mut fields = LogFields::new();
fields
.insert("user_id", "user-123")
.insert("request_id", 42);Sourcepub fn merge_with(
&mut self,
other: impl IntoIterator<Item = (Cow<'static, str>, LogValue)>,
) -> &mut Self
pub fn merge_with( &mut self, other: impl IntoIterator<Item = (Cow<'static, str>, LogValue)>, ) -> &mut Self
Merges this collection with the fields from another one.
This method borrows self and returns a mutable reference, allowing it
to be used when chaining with other methods that require borrowing.
§Merging policy
Keys in this collection with duplicate names will be overwritten by keys from the provided collection. The order of keys in the resulting collection is undefined.
§Examples
use context_logger::LogFields;
let mut fields = LogFields::new();
fields
.insert("user_id", "Alice")
.merge_with(other_fields)
.insert("request_id", 42);Trait Implementations§
Source§impl Extend<(Cow<'static, str>, LogValue)> for LogFields
impl Extend<(Cow<'static, str>, LogValue)> for LogFields
Source§fn extend<I: IntoIterator<Item = (Cow<'static, str>, LogValue)>>(
&mut self,
iter: I,
)
fn extend<I: IntoIterator<Item = (Cow<'static, str>, LogValue)>>( &mut self, iter: I, )
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)