1
+ //! Subscriber settings.
2
+
1
3
use tracing:: level_filters:: LevelFilter ;
2
4
3
5
pub mod console_log;
@@ -9,23 +11,40 @@ pub use otlp_log::*;
9
11
pub mod otlp_trace;
10
12
pub use otlp_trace:: * ;
11
13
12
- // this trait is to make it simpler to access common settings from specific settings.
14
+ /// Simplifies access common settings from subscriber specific settings.
13
15
pub trait CommonSettings {
16
+ /// Access to the [`Settings::environment_variable`] field.
14
17
fn environment_variable ( & self ) -> & ' static str ;
18
+
19
+ /// Access to the [`Settings::default_level`] field.
15
20
fn default_level ( & self ) -> LevelFilter ;
21
+
22
+ /// Access to the [`Settings::enabled`] field.
16
23
fn enabled ( & self ) -> bool ;
17
24
}
18
25
26
+ /// General settings that apply to any subscriber.
19
27
#[ derive( Debug , PartialEq ) ]
20
28
pub struct Settings {
29
+ /// The environment variable used to set the [`LevelFilter`].
30
+ ///
31
+ /// When the environment variable is set, it will override what is set by
32
+ /// [`Self::default_level`].
21
33
pub environment_variable : & ' static str ,
22
34
35
+ /// The [`LevelFilter`] to fallback to if [`Self::environment_variable`] has
36
+ /// not been set.
23
37
pub default_level : LevelFilter ,
24
38
39
+ /// Whether or not the subscriber is enabled.
40
+ ///
41
+ /// When set to `true`, the [`tracing::Subscriber`] will be added to the
42
+ /// [`tracing_subscriber::Layer`] list.
25
43
pub enabled : bool ,
26
44
}
27
45
28
46
impl Settings {
47
+ /// Builder methods to override defaults.
29
48
pub fn builder ( ) -> SettingsBuilder {
30
49
SettingsBuilder :: default ( )
31
50
}
@@ -37,13 +56,16 @@ impl Default for Settings {
37
56
}
38
57
}
39
58
59
+ /// For building [`Settings`].
40
60
pub struct SettingsBuilder {
41
61
environment_variable : & ' static str ,
42
62
enabled : bool ,
43
63
default_level : LevelFilter ,
44
64
}
45
65
66
+ /// Finalizer to be implemented on builders.
46
67
pub trait Build < T > {
68
+ /// Finalize settings.
47
69
fn build ( self ) -> T ;
48
70
}
49
71
@@ -54,16 +76,27 @@ impl Build<Settings> for SettingsBuilder {
54
76
}
55
77
56
78
impl SettingsBuilder {
79
+ /// Set the environment variable used for overriding the [`Settings::default_level`].
80
+ ///
81
+ /// Defaults to `RUST_LOG`.
82
+ // TODO (@NickLarsenNZ): set a constant for the default environment variable.
57
83
pub fn with_environment_variable ( mut self , name : & ' static str ) -> Self {
58
84
self . environment_variable = name;
59
85
self
60
86
}
61
87
88
+ /// Set the default [`LevelFilter`].
89
+ ///
90
+ /// Defaults to [`LevelFilter::OFF`].
91
+ // TODO (@NickLarsenNZ): set a constant for the default level.
62
92
pub fn with_default_level ( mut self , level : impl Into < LevelFilter > ) -> Self {
63
93
self . default_level = level. into ( ) ;
64
94
self
65
95
}
66
96
97
+ /// Enable or disable the [`tracing::Subscriber`].
98
+ ///
99
+ /// Defaults to `false`.
67
100
// TODO (@NickLarsenNZ): Currently this has to be called to enable the
68
101
// subscriber. Eventually it should become optional, and default to on (if
69
102
// settings are supplied). Therefore, the fields in TracingBuilder to hold
@@ -75,14 +108,17 @@ impl SettingsBuilder {
75
108
self
76
109
}
77
110
111
+ /// Set specific [`ConsoleLogSettings`].
78
112
pub fn console_log_settings_builder ( self ) -> ConsoleLogSettingsBuilder {
79
113
self . into ( )
80
114
}
81
115
116
+ /// Set specific [`OtlpLogSettings`].
82
117
pub fn otlp_log_settings_builder ( self ) -> OtlpLogSettingsBuilder {
83
118
self . into ( )
84
119
}
85
120
121
+ /// Set specific [`OtlpTraceSettings`].
86
122
pub fn otlp_trace_settings_builder ( self ) -> OtlpTraceSettingsBuilder {
87
123
self . into ( )
88
124
}
0 commit comments