Trait slog::KV[][src]

pub trait KV {
    fn serialize(&self, record: &Record, serializer: &mut Serializer) -> Result;
}

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:

This example is not tested
info!(logger, "my event"; "type_key" => %my_val);

If you implement this trait, that can become:

This example is not tested
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

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]

impl KV for ()
[src]

impl<T: KV, R: KV> KV for (T, R)
[src]

impl<T: ?Sized> KV for Box<T> where
    T: KV
[src]

impl<T: ?Sized> KV for Arc<T> where
    T: KV
[src]

Implementors