diff --git a/src/bootstrap/logging.rs b/src/bootstrap/logging.rs index 8546720f..4fc3ead9 100644 --- a/src/bootstrap/logging.rs +++ b/src/bootstrap/logging.rs @@ -6,14 +6,15 @@ //! - `Info` //! - `Debug` //! - `Trace` -use std::str::FromStr; use std::sync::Once; use log::{info, LevelFilter}; +use crate::config::v1::LogLevel; + static INIT: Once = Once::new(); -pub fn setup(log_level: &Option) { +pub fn setup(log_level: &Option) { let level = config_level_or_default(log_level); if level == log::LevelFilter::Off { @@ -25,10 +26,17 @@ pub fn setup(log_level: &Option) { }); } -fn config_level_or_default(log_level: &Option) -> LevelFilter { +fn config_level_or_default(log_level: &Option) -> LevelFilter { match log_level { None => log::LevelFilter::Info, - Some(level) => LevelFilter::from_str(level).unwrap(), + Some(level) => match level { + LogLevel::Off => LevelFilter::Off, + LogLevel::Error => LevelFilter::Error, + LogLevel::Warn => LevelFilter::Warn, + LogLevel::Info => LevelFilter::Info, + LogLevel::Debug => LevelFilter::Debug, + LogLevel::Trace => LevelFilter::Trace, + }, } } diff --git a/src/config/v1/mod.rs b/src/config/v1/mod.rs index fea3e2ae..f5e95bc1 100644 --- a/src/config/v1/mod.rs +++ b/src/config/v1/mod.rs @@ -26,7 +26,7 @@ use super::validator::{ValidationError, Validator}; pub struct Settings { /// Logging level. Possible values are: `Off`, `Error`, `Warn`, `Info`, /// `Debug` and `Trace`. Default is `Info`. - pub log_level: Option, + pub log_level: Option, /// The website customizable values. pub website: Website, /// The tracker configuration. @@ -71,3 +71,20 @@ impl Validator for Settings { self.tracker.validate() } } + +#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Clone)] +#[serde(rename_all = "lowercase")] +pub enum LogLevel { + /// A level lower than all log levels. + Off, + /// Corresponds to the `Error` log level. + Error, + /// Corresponds to the `Warn` log level. + Warn, + /// Corresponds to the `Info` log level. + Info, + /// Corresponds to the `Debug` log level. + Debug, + /// Corresponds to the `Trace` log level. + Trace, +} diff --git a/tests/environments/isolated.rs b/tests/environments/isolated.rs index 7a389930..d74ee48e 100644 --- a/tests/environments/isolated.rs +++ b/tests/environments/isolated.rs @@ -1,5 +1,6 @@ use tempfile::TempDir; use torrust_index::config; +use torrust_index::config::v1::LogLevel; use torrust_index::config::FREE_PORT; use torrust_index::web::api::Version; use url::Url; @@ -72,7 +73,7 @@ impl Default for TestEnv { /// Provides a configuration with ephemeral data for testing. fn ephemeral(temp_dir: &TempDir) -> config::Settings { let mut configuration = config::Settings { - log_level: Some("off".to_owned()), // Change to `debug` for tests debugging + log_level: Some(LogLevel::Off), // Change to `debug` for tests debugging ..config::Settings::default() };