stackable_telemetry/tracing/settings/
console_log.rs

1//! Console Log Subscriber Settings.
2
3use tracing::level_filters::LevelFilter;
4
5use super::{Settings, SettingsBuilder, SettingsToggle};
6
7/// Configure specific settings for the console log subscriber.
8#[derive(Debug, Default, PartialEq)]
9pub enum ConsoleLogSettings {
10    /// Console subscriber disabled.
11    #[default]
12    Disabled,
13
14    /// Console subscriber enabled.
15    Enabled {
16        /// Common subscriber settings that apply to the Console Log Subscriber.
17        common_settings: Settings,
18
19        /// Console Subscriber log event output format.
20        log_format: Format,
21    },
22}
23
24/// Console subscriber log event output formats.
25///
26/// Currently, only [Plain][Format::Plain] is supported.
27#[derive(Clone, Debug, Default, Eq, PartialEq, strum::EnumString, strum::Display)]
28#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
29#[strum(serialize_all = "snake_case")]
30pub enum Format {
31    /// Use the plain unstructured log output.
32    ///
33    /// ANSI color output is enabled by default, but can be disabled at runtime by
34    /// setting `NO_COLOR` to a non-empty value.
35    ///
36    /// See: [`Layer::with_ansi`][tracing_subscriber::fmt::Layer::with_ansi].
37    #[default]
38    Plain,
39
40    /// Use structured JSON log output.
41    Json,
42    // LogFmt,
43}
44
45impl SettingsToggle for ConsoleLogSettings {
46    fn is_enabled(&self) -> bool {
47        match self {
48            ConsoleLogSettings::Disabled => false,
49            ConsoleLogSettings::Enabled { .. } => true,
50        }
51    }
52}
53
54/// For building [`ConsoleLogSettings`].
55///
56/// <div class="warning">
57/// Do not use directly, instead use the [`Settings::builder`] associated function.
58/// </div>
59pub struct ConsoleLogSettingsBuilder {
60    pub(crate) common_settings: Settings,
61    pub(crate) log_format: Format,
62}
63
64impl ConsoleLogSettingsBuilder {
65    /// Overrides the default log [`Format`].
66    pub fn with_log_format(mut self, format: Format) -> Self {
67        self.log_format = format;
68        self
69    }
70
71    /// Consumes `self` and builds [`ConsoleLogSettings`].
72    pub fn build(self) -> ConsoleLogSettings {
73        ConsoleLogSettings::Enabled {
74            common_settings: self.common_settings,
75            log_format: self.log_format,
76        }
77    }
78}
79
80/// This implementation is used to turn the common settings builder into the console log specific
81/// settings builder via the [`SettingsBuilder::console_log_settings_builder`] function.
82impl From<SettingsBuilder> for ConsoleLogSettingsBuilder {
83    fn from(value: SettingsBuilder) -> Self {
84        Self {
85            common_settings: value.build(),
86            log_format: Format::default(),
87        }
88    }
89}
90
91impl From<Settings> for ConsoleLogSettings {
92    fn from(common_settings: Settings) -> Self {
93        ConsoleLogSettings::Enabled {
94            common_settings,
95            log_format: Default::default(),
96        }
97    }
98}
99
100impl<T> From<Option<T>> for ConsoleLogSettings
101where
102    T: Into<ConsoleLogSettings>,
103{
104    fn from(settings: Option<T>) -> Self {
105        match settings {
106            Some(settings) => settings.into(),
107            None => ConsoleLogSettings::default(),
108        }
109    }
110}
111
112impl From<(&'static str, LevelFilter)> for ConsoleLogSettings {
113    fn from(value: (&'static str, LevelFilter)) -> Self {
114        Self::Enabled {
115            common_settings: Settings {
116                environment_variable: value.0,
117                default_level: value.1,
118            },
119            log_format: Default::default(),
120        }
121    }
122}
123
124impl From<(&'static str, LevelFilter, bool)> for ConsoleLogSettings {
125    fn from(value: (&'static str, LevelFilter, bool)) -> Self {
126        match value.2 {
127            true => Self::Enabled {
128                common_settings: Settings {
129                    environment_variable: value.0,
130                    default_level: value.1,
131                },
132                log_format: Default::default(),
133            },
134            false => Self::Disabled,
135        }
136    }
137}
138
139#[cfg(test)]
140mod test {
141    use tracing::level_filters::LevelFilter;
142
143    use super::*;
144
145    #[test]
146    fn builds_settings() {
147        let expected = ConsoleLogSettings::Enabled {
148            common_settings: Settings {
149                environment_variable: "hello",
150                default_level: LevelFilter::DEBUG,
151            },
152            log_format: Format::Plain,
153        };
154        let result = Settings::builder()
155            .with_environment_variable("hello")
156            .with_default_level(LevelFilter::DEBUG)
157            .console_log_settings_builder()
158            .with_log_format(Format::Plain)
159            // color
160            .build();
161
162        assert_eq!(expected, result);
163    }
164}