Skip to content

Commit

Permalink
aj/fix log level casing (#372)
Browse files Browse the repository at this point in the history
* feat: serde's rename_all isn't working, use a custom deserializer to lowercase loglevels

* feat: default is warn

* feat: Allow reptition to clear up imports

* feat: rebase
  • Loading branch information
astuyve authored Aug 23, 2024
1 parent 8a0cf8f commit c945b27
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
23 changes: 21 additions & 2 deletions bottlecap/src/config/log_level.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::Deserialize;
use serde::{Deserialize, Deserializer};
use tracing::error;

#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum LogLevel {
/// Designates very serious errors.
Error,
Expand Down Expand Up @@ -41,3 +41,22 @@ impl LogLevel {
}
}
}

#[allow(clippy::module_name_repetitions)]
pub fn deserialize_log_level<'de, D>(deserializer: D) -> Result<LogLevel, D::Error>
where
D: Deserializer<'de>,
{
let s: String = Deserialize::deserialize(deserializer)?;
match s.to_lowercase().as_str() {
"error" => Ok(LogLevel::Error),
"warn" => Ok(LogLevel::Warn),
"info" => Ok(LogLevel::Info),
"debug" => Ok(LogLevel::Debug),
"trace" => Ok(LogLevel::Trace),
_ => {
error!("Unknown log level: {}, using warn", s);
Ok(LogLevel::Warn)
}
}
}
22 changes: 21 additions & 1 deletion bottlecap/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use figment::{providers::Env, Figment};
use serde::Deserialize;

use crate::config::flush_strategy::FlushStrategy;
use crate::config::log_level::LogLevel;
use crate::config::log_level::{deserialize_log_level, LogLevel};
use crate::config::processing_rule::{deserialize_processing_rules, ProcessingRule};

/// `FailoverConfig` is a struct that represents fields that are not supported in the extension yet.
Expand Down Expand Up @@ -55,6 +55,7 @@ pub struct Config {
pub service: Option<String>,
pub version: Option<String>,
pub tags: Option<String>,
#[serde(deserialize_with = "deserialize_log_level")]
pub log_level: LogLevel,
#[serde(deserialize_with = "deserialize_processing_rules")]
pub logs_config_processing_rules: Option<Vec<ProcessingRule>>,
Expand Down Expand Up @@ -311,6 +312,25 @@ pub mod tests {
});
}

#[test]
fn test_parse_log_level() {
figment::Jail::expect_with(|jail| {
jail.clear_env();
jail.set_env("DD_LOG_LEVEL", "TRACE");
jail.set_env("DD_EXTENSION_VERSION", "next");
let config = get_config(Path::new("")).expect("should parse config");
assert_eq!(
config,
Config {
log_level: LogLevel::Trace,
site: "datadoghq.com".to_string(),
..Config::default()
}
);
Ok(())
});
}

#[test]
fn test_parse_default() {
figment::Jail::expect_with(|jail| {
Expand Down

0 comments on commit c945b27

Please sign in to comment.