-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathconsole_log.rs
165 lines (145 loc) · 4.53 KB
/
console_log.rs
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//! Console Log Subscriber Settings.
use tracing::level_filters::LevelFilter;
use super::{Settings, SettingsBuilder, SettingsToggle};
/// Configure specific settings for the console log subscriber.
#[derive(Debug, Default, PartialEq)]
pub enum ConsoleLogSettings {
/// Console subscriber disabled.
#[default]
Disabled,
/// Console subscriber enabled.
Enabled {
/// Common subscriber settings that apply to the Console Log Subscriber.
common_settings: Settings,
/// Console Subscriber log event output format.
log_format: Format,
},
}
/// Console subscriber log event output formats.
///
/// Currently, only [Plain][Format::Plain] is supported.
#[derive(
Clone, Debug, Default, Eq, PartialEq, strum::EnumString, strum::Display, clap::ValueEnum,
)]
#[strum(serialize_all = "snake_case")]
pub enum Format {
/// Use the plain unstructured log output.
///
/// ANSI color output is enabled by default, but can be disabled at runtime by
/// setting `NO_COLOR` to a non-empty value.
///
/// See: [`Layer::with_ansi`][tracing_subscriber::fmt::Layer::with_ansi].
#[default]
Plain,
/// Use structured JSON log output.
Json,
// LogFmt,
}
impl SettingsToggle for ConsoleLogSettings {
fn is_enabled(&self) -> bool {
match self {
ConsoleLogSettings::Disabled => false,
ConsoleLogSettings::Enabled { .. } => true,
}
}
}
/// For building [`ConsoleLogSettings`].
///
/// <div class="warning">
/// Do not use directly, instead use the [`Settings::builder`] associated function.
/// </div>
pub struct ConsoleLogSettingsBuilder {
pub(crate) common_settings: Settings,
pub(crate) log_format: Format,
}
impl ConsoleLogSettingsBuilder {
/// Overrides the default log [`Format`].
pub fn with_log_format(mut self, format: Format) -> Self {
self.log_format = format;
self
}
/// Consumes `self` and builds [`ConsoleLogSettings`].
pub fn build(self) -> ConsoleLogSettings {
ConsoleLogSettings::Enabled {
common_settings: self.common_settings,
log_format: self.log_format,
}
}
}
/// This implementation is used to turn the common settings builder into the console log specific
/// settings builder via the [`SettingsBuilder::console_log_settings_builder`] function.
impl From<SettingsBuilder> for ConsoleLogSettingsBuilder {
fn from(value: SettingsBuilder) -> Self {
Self {
common_settings: value.build(),
log_format: Format::default(),
}
}
}
impl From<Settings> for ConsoleLogSettings {
fn from(common_settings: Settings) -> Self {
ConsoleLogSettings::Enabled {
common_settings,
log_format: Default::default(),
}
}
}
impl<T> From<Option<T>> for ConsoleLogSettings
where
T: Into<ConsoleLogSettings>,
{
fn from(settings: Option<T>) -> Self {
match settings {
Some(settings) => settings.into(),
None => ConsoleLogSettings::default(),
}
}
}
impl From<(&'static str, LevelFilter)> for ConsoleLogSettings {
fn from(value: (&'static str, LevelFilter)) -> Self {
Self::Enabled {
common_settings: Settings {
environment_variable: value.0,
default_level: value.1,
},
log_format: Default::default(),
}
}
}
impl From<(&'static str, LevelFilter, bool)> for ConsoleLogSettings {
fn from(value: (&'static str, LevelFilter, bool)) -> Self {
match value.2 {
true => Self::Enabled {
common_settings: Settings {
environment_variable: value.0,
default_level: value.1,
},
log_format: Default::default(),
},
false => Self::Disabled,
}
}
}
#[cfg(test)]
mod test {
use tracing::level_filters::LevelFilter;
use super::*;
#[test]
fn builds_settings() {
let expected = ConsoleLogSettings::Enabled {
common_settings: Settings {
environment_variable: "hello",
default_level: LevelFilter::DEBUG,
},
log_format: Format::Plain,
};
let result = Settings::builder()
.with_environment_variable("hello")
.with_default_level(LevelFilter::DEBUG)
.console_log_settings_builder()
.with_log_format(Format::Plain)
// color
.build();
assert_eq!(expected, result);
}
}