JsonLogger

Struct JsonLogger 

Source
pub struct JsonLogger { /* private fields */ }
Expand description

This logger is a implementation for log::Log trait.

Implementations§

Source§

impl JsonLogger

Source

pub fn init(output: LoggerOutput, filter: LogLevelFilter)

#[macro_use]
extern crate log;
#[macro_use]
extern crate s_structured_log;
extern crate serde_json;

use log::LogLevelFilter;
use s_structured_log::{JsonLogger, LoggerOutput};

fn main() {
    JsonLogger::init(LoggerOutput::Stderr, LogLevelFilter::Info);

    s_info!(json_object! {
        "key" => "value"
    });
}
Examples found in repository?
examples/basic.rs (line 10)
9fn main() {
10    JsonLogger::init(LoggerOutput::Stdout, log::LogLevelFilter::Info);
11
12    s_trace!(json_object! {
13        "trace_key1" => 1,
14        "trace_key2" => "value2"
15    });
16    s_debug!(json_object! {
17        "debug_key1" => 1,
18        "debug_key2" => "value2"
19    });
20    s_info!(json_object! {
21        "info_key1" => 1,
22        "info_key2" => "value2"
23    });
24    s_warn!(json_object! {
25        "warn_key1" => 1,
26        "warn_key2" => "value2"
27    });
28    s_error!(json_object! {
29        "error_key1" => 1,
30        "error_key2" => "value2"
31    });
32
33    trace!("{:?}",
34           json_object! {
35        "trace_key1" => 1,
36        "trace_key2" => "value2"
37    });
38    error!("{}",
39           json_format! {
40        "error_key1" => 1,
41        "error_key2" => q("value2"),
42        "error_key3" => json_format![q("value3"),4]
43    });
44}
More examples
Hide additional examples
examples/complicated_json.rs (line 10)
9fn main() {
10    JsonLogger::init(LoggerOutput::Stderr, log::LogLevelFilter::Info);
11
12    // use json_object!
13    s_info!(json_object! {
14        "Fruits" => json_object! {
15            "on_the_table" => json_object! {
16                "Apple" => 1,
17                "Orange" => "two",
18                "Grape" => 1.2
19            },
20            "in_the_basket" => ["Banana", "Strawberry"]
21        },
22        "Pets" => [
23            json_object! {
24                "name" => "Tama",
25                "kind" => "cat",
26                "age" => 3
27            },
28            json_object! {
29                "name" => "Pochi",
30                "kind" => "dog",
31                "age" => 5
32            }
33        ]
34    });
35
36    // use json_format! and target with `json:` prefix.
37    info!(target: &format!("json:{}", module_path!()),
38          "{}",
39          json_format! {
40        "Fruits" => json_format! {
41            "on_the_table" => json_format! {
42                "Apple" => 1,
43                "Orange" => q("two"),
44                "Grape" => 1.2
45            },
46            "in_the_basket" => json_format![q("Banana"), q("Strawberry")]
47        },
48        "Pets" => json_format![
49            json_format! {
50                "name" => q("Tama"),
51                "kind" => q("cat"),
52                "age" => 3
53            },
54            json_format! {
55                "name" => q("Pochi"),
56                "kind" => q("dog"),
57                "age" => 5
58            }
59        ]
60    });
61
62    // use json_format! and default target.
63    info!("{}",
64          json_format! {
65        "Fruits" => json_format! {
66            "on_the_table" => json_format! {
67                "Apple" => 1,
68                "Orange" => 2,
69                "Grape" => 1.2
70            },
71            "in_the_basket" => json_format![q("Banana"), q("Strawberry")]
72        },
73        "Pets" => json_format![
74            json_format! {
75                "name" => q("Tama"),
76                "kind" => q("cat")
77            },
78            json_format! {
79                "name" => q("Pochi"),
80                "kind" => q("dog")
81            }
82        ]
83    });
84}

Trait Implementations§

Source§

impl Log for JsonLogger

Source§

fn enabled(&self, metadata: &LogMetadata<'_>) -> bool

Determines if a log message with the specified metadata would be logged. Read more
Source§

fn log(&self, record: &LogRecord<'_>)

Logs the LogRecord. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.