From c945b272a37ee31243980fecda098607f121ce36 Mon Sep 17 00:00:00 2001 From: AJ Stuyvenberg Date: Fri, 23 Aug 2024 10:10:15 -0400 Subject: [PATCH] aj/fix log level casing (#372) * 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 --- bottlecap/src/config/log_level.rs | 23 +++++++++++++++++++++-- bottlecap/src/config/mod.rs | 22 +++++++++++++++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/bottlecap/src/config/log_level.rs b/bottlecap/src/config/log_level.rs index 03f1ddb3..86c255e1 100644 --- a/bottlecap/src/config/log_level.rs +++ b/bottlecap/src/config/log_level.rs @@ -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, @@ -41,3 +41,22 @@ impl LogLevel { } } } + +#[allow(clippy::module_name_repetitions)] +pub fn deserialize_log_level<'de, D>(deserializer: D) -> Result +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) + } + } +} diff --git a/bottlecap/src/config/mod.rs b/bottlecap/src/config/mod.rs index 748a381f..a9571b77 100644 --- a/bottlecap/src/config/mod.rs +++ b/bottlecap/src/config/mod.rs @@ -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. @@ -55,6 +55,7 @@ pub struct Config { pub service: Option, pub version: Option, pub tags: Option, + #[serde(deserialize_with = "deserialize_log_level")] pub log_level: LogLevel, #[serde(deserialize_with = "deserialize_processing_rules")] pub logs_config_processing_rules: Option>, @@ -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| {