-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmod.rs
148 lines (126 loc) · 4.05 KB
/
mod.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
//! Subscriber settings.
use tracing::level_filters::LevelFilter;
pub mod console_log;
pub use console_log::*;
pub mod otlp_log;
pub use otlp_log::*;
pub mod otlp_trace;
pub use otlp_trace::*;
/// General settings that apply to any subscriber.
#[derive(Debug, PartialEq)]
pub struct Settings {
/// The environment variable used to set the [`LevelFilter`].
///
/// When the environment variable is set, it will override what is set by
/// [`Self::default_level`].
pub environment_variable: &'static str,
/// The [`LevelFilter`] to fallback to if [`Self::environment_variable`] has
/// not been set.
pub default_level: LevelFilter,
/// Whether or not the subscriber is enabled.
///
/// When set to `true`, the [`tracing::Subscriber`] will be added to the
/// [`tracing_subscriber::Layer`] list.
pub enabled: bool,
}
impl Settings {
/// Builder methods to override defaults.
pub fn builder() -> SettingsBuilder {
SettingsBuilder::default()
}
}
impl Default for Settings {
fn default() -> Self {
SettingsBuilder::default().build()
}
}
/// For building [`Settings`].
pub struct SettingsBuilder {
environment_variable: &'static str,
enabled: bool,
default_level: LevelFilter,
}
/// Finalizer to be implemented on builders.
pub trait Build<T> {
/// Finalize settings.
fn build(self) -> T;
}
impl Build<Settings> for SettingsBuilder {
fn build(self) -> Settings {
Settings {
environment_variable: self.environment_variable,
default_level: self.default_level,
enabled: self.enabled,
}
}
}
impl SettingsBuilder {
/// Set the environment variable used for overriding the [`Settings::default_level`].
///
/// Defaults to `RUST_LOG`.
// TODO (@NickLarsenNZ): set a constant for the default environment variable.
pub fn with_environment_variable(mut self, name: &'static str) -> Self {
self.environment_variable = name;
self
}
/// Set the default [`LevelFilter`].
///
/// Defaults to [`LevelFilter::OFF`].
// TODO (@NickLarsenNZ): set a constant for the default level.
pub fn with_default_level(mut self, level: impl Into<LevelFilter>) -> Self {
self.default_level = level.into();
self
}
/// Enable or disable the [`tracing::Subscriber`].
///
/// Defaults to `false`.
// TODO (@NickLarsenNZ): Currently this has to be called to enable the
// subscriber. Eventually it should become optional, and default to on (if
// settings are supplied). Therefore, the fields in TracingBuilder to hold
// the subscriber settings should become Option<T> so that the subscriber is
// disabled when not configured, is enabled when configured, while still
// controllable through this function. Then this can be renamed to `with_enabled`
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
/// Set specific [`ConsoleLogSettings`].
pub fn console_log_settings_builder(self) -> ConsoleLogSettingsBuilder {
self.into()
}
/// Set specific [`OtlpLogSettings`].
pub fn otlp_log_settings_builder(self) -> OtlpLogSettingsBuilder {
self.into()
}
/// Set specific [`OtlpTraceSettings`].
pub fn otlp_trace_settings_builder(self) -> OtlpTraceSettingsBuilder {
self.into()
}
}
impl Default for SettingsBuilder {
fn default() -> Self {
Self {
environment_variable: "RUST_LOG",
default_level: LevelFilter::OFF,
enabled: false,
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn builds_settings() {
let expected = Settings {
environment_variable: "hello",
default_level: LevelFilter::DEBUG,
enabled: true,
};
let result = Settings::builder()
.with_environment_variable("hello")
.with_default_level(LevelFilter::DEBUG)
.enabled(true)
.build();
assert_eq!(expected, result);
}
}