stackable_telemetry/tracing/settings/
console_log.rsuse tracing::level_filters::LevelFilter;
use super::{Settings, SettingsBuilder, SettingsToggle};
#[derive(Debug, Default, PartialEq)]
pub enum ConsoleLogSettings {
#[default]
Disabled,
Enabled {
common_settings: Settings,
log_format: Format,
},
}
#[derive(Clone, Debug, Default, Eq, PartialEq, strum::EnumString, strum::Display)]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
#[strum(serialize_all = "snake_case")]
pub enum Format {
#[default]
Plain,
Json,
}
impl SettingsToggle for ConsoleLogSettings {
fn is_enabled(&self) -> bool {
match self {
ConsoleLogSettings::Disabled => false,
ConsoleLogSettings::Enabled { .. } => true,
}
}
}
pub struct ConsoleLogSettingsBuilder {
pub(crate) common_settings: Settings,
pub(crate) log_format: Format,
}
impl ConsoleLogSettingsBuilder {
pub fn with_log_format(mut self, format: Format) -> Self {
self.log_format = format;
self
}
pub fn build(self) -> ConsoleLogSettings {
ConsoleLogSettings::Enabled {
common_settings: self.common_settings,
log_format: self.log_format,
}
}
}
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)
.build();
assert_eq!(expected, result);
}
}