From 00be925c568e3dfa2e7229c03c09b0deaab7a403 Mon Sep 17 00:00:00 2001 From: Thibault Cheneviere Date: Thu, 1 Aug 2024 23:46:31 -0400 Subject: [PATCH 1/2] feat: made the log config part optional and with default values --- src/config.rs | 58 ++++++++++++++++++++++++++++++++++++++------------- src/main.rs | 8 +++---- 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/src/config.rs b/src/config.rs index 19f1215..347ed54 100644 --- a/src/config.rs +++ b/src/config.rs @@ -10,7 +10,8 @@ pub const BASE_CONFIG_FILE: &str = "/etc/dnsr/config.yml"; #[derive(Deserialize, Clone, Debug)] pub struct Config { - pub log: LogConfig, + log: Option, + pub keys: Keys, } @@ -22,6 +23,10 @@ impl Config { pub fn tsig_path(&self) -> &Path { Path::new(TSIG_PATH) } + + pub fn log_config(&self) -> LogConfig { + self.log.unwrap_or_default() + } } impl TryFrom<&Vec> for Config { @@ -32,27 +37,50 @@ impl TryFrom<&Vec> for Config { } } -#[derive(Deserialize, Clone, Copy, Debug)] +#[derive(Deserialize, Default, Clone, Copy, Debug)] pub struct LogConfig { - #[serde(deserialize_with = "de_level_filter")] - pub level: log::LevelFilter, - pub enable_metrics: bool, - pub enable_thread_id: bool, - pub stderr: bool, + #[serde(deserialize_with = "de_opt_level_filter")] + level: Option, + enable_metrics: Option, + enable_thread_id: Option, + stderr: Option, +} + +impl LogConfig { + pub fn level(&self) -> log::LevelFilter { + self.level.unwrap_or(log::LevelFilter::Info) + } + + pub fn enable_metrics(&self) -> bool { + self.enable_metrics.unwrap_or(true) + } + + pub fn enable_thread_id(&self) -> bool { + self.enable_thread_id.unwrap_or(false) + } + + pub fn stderr(&self) -> bool { + self.stderr.unwrap_or(false) + } } -fn de_level_filter<'de, D>(deserializer: D) -> std::result::Result +fn de_opt_level_filter<'de, D>( + deserializer: D, +) -> std::result::Result, D::Error> where D: serde::Deserializer<'de>, { - let s: LevelFilter = Deserialize::deserialize(deserializer)?; + let s: Option = Deserialize::deserialize(deserializer)?; + let Some(s) = s else { + return Ok(None); + }; match s { - LevelFilter::Off => Ok(log::LevelFilter::Off), - LevelFilter::Error => Ok(log::LevelFilter::Error), - LevelFilter::Warn => Ok(log::LevelFilter::Warn), - LevelFilter::Info => Ok(log::LevelFilter::Info), - LevelFilter::Debug => Ok(log::LevelFilter::Debug), - LevelFilter::Trace => Ok(log::LevelFilter::Trace), + LevelFilter::Off => Ok(Some(log::LevelFilter::Off)), + LevelFilter::Error => Ok(Some(log::LevelFilter::Error)), + LevelFilter::Warn => Ok(Some(log::LevelFilter::Warn)), + LevelFilter::Info => Ok(Some(log::LevelFilter::Info)), + LevelFilter::Debug => Ok(Some(log::LevelFilter::Debug)), + LevelFilter::Trace => Ok(Some(log::LevelFilter::Trace)), } } diff --git a/src/main.rs b/src/main.rs index ec45047..c807609 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,10 +61,10 @@ async fn main() { // Initialize the custom logger logger::Logger::new() - .with_level(config.log.level) - .with_metrics(config.log.enable_metrics) - .with_stderr(config.log.stderr) - .with_thread(config.log.enable_thread_id) + .with_level(config.log_config().level()) + .with_metrics(config.log_config().enable_metrics()) + .with_stderr(config.log_config().stderr()) + .with_thread(config.log_config().enable_thread_id()) .init() .expect("Failed to initialize custom logger"); From 1551c92bac2dab0436a3d594e12515e429377a00 Mon Sep 17 00:00:00 2001 From: Thibault Cheneviere Date: Thu, 1 Aug 2024 23:47:41 -0400 Subject: [PATCH 2/2] feat: updated log config doc --- README.md | 4 +++- config.yml | 8 +++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b427576..bcd137c 100644 --- a/README.md +++ b/README.md @@ -25,13 +25,15 @@ The `config.yml` file is used to configure the `dnsr server. The following is th # This file is located at the path in the `DNSR_CONFIG` environment variable or in the `/etc/dnsr/config.yml` file. # The log configuration. +# This part is optional and every field is optional. +# If not present, the values below are used as defaults. log: # The log level. This can be one of the following: trace, debug, info, warn, error, or off. level: info # Enable the metrics. enable_metrics: true # Enable thread ID in logs. - enable_thread_id: true + enable_thread_id: false # Log on stderr. stderr: false diff --git a/config.yml b/config.yml index 2e2eda1..47037c7 100644 --- a/config.yml +++ b/config.yml @@ -2,13 +2,15 @@ # This file is located at the path in the `DNSR_CONFIG` environment variable or in the `config.yml` file in the current directory. # The log configuration. +# This part is optional and every field is optional. +# If not present, the values below are used as defaults. log: # The log level. This can be one of the following: trace, debug, info, warn, error, or off. - level: debug + level: info # Enable the metrics. - enable_metrics: false + enable_metrics: true # Enable thread ID in logs. - enable_thread_id: true + enable_thread_id: false # Log on stderr. stderr: false