1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use thiserror::Error;
pub use tracing_subscriber::{util::TryInitError as LogInitError, EnvFilter as LogFilter};
#[derive(Clone, Debug)]
#[cfg_attr(docsrs, doc(cfg(feature = "log")))]
pub enum LogFormat {
Plain,
Json,
}
#[derive(Debug, Error)]
#[error("invalid log level: {0} must be 'plain' or 'json'")]
#[cfg_attr(docsrs, doc(cfg(feature = "log")))]
pub struct InvalidLogFormat(String);
impl Default for LogFormat {
fn default() -> Self {
Self::Plain
}
}
impl std::str::FromStr for LogFormat {
type Err = InvalidLogFormat;
fn from_str(s: &str) -> Result<Self, InvalidLogFormat> {
match s {
"json" => Ok(LogFormat::Json),
"plain" => Ok(LogFormat::Plain),
s => Err(InvalidLogFormat(s.to_string())),
}
}
}
impl LogFormat {
pub fn try_init(self, filter: LogFilter) -> Result<(), LogInitError> {
use tracing_subscriber::prelude::*;
let registry = tracing_subscriber::registry().with(filter);
match self {
LogFormat::Plain => registry.with(tracing_subscriber::fmt::layer()).try_init()?,
LogFormat::Json => {
let event_fmt = tracing_subscriber::fmt::format()
.json()
.with_span_list(true)
.with_current_span(false);
let fmt = tracing_subscriber::fmt::layer()
.event_format(event_fmt)
.fmt_fields(tracing_subscriber::fmt::format::JsonFields::default());
registry.with(fmt).try_init()?
}
};
Ok(())
}
}