Trait slog::KV [−][src]
Key-value pair(s) for log events
Zero, one or more key value pairs chained together
Any logging data must implement this trait for slog to be able to use it,
although slog comes with default implementations within its macros (the
=>
and kv!
portions of the log macros).
If you don’t use this trait, you must emit your structured data by specifying both key and value in each log event:
info!(logger, "my event"; "type_key" => %my_val);
If you implement this trait, that can become:
info!(logger, "my event"; my_val);
Types implementing this trait can emit multiple key-value pairs, and can customize their structured representation. The order of emitting them should be consistent with the way key-value pair hierarchy is traversed: from data most specific to the logging context to the most general one. Or in other words: from newest to oldest.
Implementers are are responsible for calling the emit_*
methods on the
Serializer
passed in, the Record
can be used to make display decisions
based on context, but for most plain-value structs you will just call
emit_*
.
Example
use slog::{KV, Record, Result, Serializer}; struct MyNewType(i64); impl KV for MyNewType { fn serialize(&self, _rec: &Record, serializer: &mut Serializer) -> Result { serializer.emit_i64("my_new_type", self.0) } }
See also Value
, which allows you to customize just
the right hand side of the =>
structure macro, and (if you have the
nested-values
feature enabled) SerdeValue
which allows emitting anything serde can emit.
Required methods
fn serialize(
&self,
record: &Record<'_>,
serializer: &mut dyn Serializer
) -> Result
[src]
&self,
record: &Record<'_>,
serializer: &mut dyn Serializer
) -> Result
Serialize self into Serializer
KV
should call respective Serializer
methods
for each key-value pair it contains.
Implementations on Foreign Types
impl<'a, T> KV for &'a T where
T: KV,
[src]
T: KV,
fn serialize(
&self,
record: &Record<'_>,
serializer: &mut dyn Serializer
) -> Result
[src]
&self,
record: &Record<'_>,
serializer: &mut dyn Serializer
) -> Result
impl KV for ()
[src]
fn serialize(
&self,
_record: &Record<'_>,
_serializer: &mut dyn Serializer
) -> Result
[src]
&self,
_record: &Record<'_>,
_serializer: &mut dyn Serializer
) -> Result
impl<T: KV, R: KV> KV for (T, R)
[src]
fn serialize(
&self,
record: &Record<'_>,
serializer: &mut dyn Serializer
) -> Result
[src]
&self,
record: &Record<'_>,
serializer: &mut dyn Serializer
) -> Result
impl<T: ?Sized> KV for Box<T> where
T: KV,
[src]
T: KV,
fn serialize(
&self,
record: &Record<'_>,
serializer: &mut dyn Serializer
) -> Result
[src]
&self,
record: &Record<'_>,
serializer: &mut dyn Serializer
) -> Result
impl<T: ?Sized> KV for Arc<T> where
T: KV,
[src]
T: KV,
fn serialize(
&self,
record: &Record<'_>,
serializer: &mut dyn Serializer
) -> Result
[src]
&self,
record: &Record<'_>,
serializer: &mut dyn Serializer
) -> Result
Implementors
impl KV for OwnedKVList
[src]
fn serialize(
&self,
record: &Record<'_>,
serializer: &mut dyn Serializer
) -> Result
[src]
&self,
record: &Record<'_>,
serializer: &mut dyn Serializer
) -> Result
impl<'a> KV for BorrowedKV<'a>
[src]
fn serialize(
&self,
record: &Record<'_>,
serializer: &mut dyn Serializer
) -> Result
[src]
&self,
record: &Record<'_>,
serializer: &mut dyn Serializer
) -> Result
impl<T: ?Sized> KV for OwnedKV<T> where
T: SendSyncRefUnwindSafeKV,
[src]
T: SendSyncRefUnwindSafeKV,
fn serialize(
&self,
record: &Record<'_>,
serializer: &mut dyn Serializer
) -> Result
[src]
&self,
record: &Record<'_>,
serializer: &mut dyn Serializer
) -> Result
impl<V> KV for SingleKV<V> where
V: Value,
[src]
V: Value,
fn serialize(
&self,
record: &Record<'_>,
serializer: &mut dyn Serializer
) -> Result
[src]
&self,
record: &Record<'_>,
serializer: &mut dyn Serializer
) -> Result