Skip to content

Commit

Permalink
refactor: [#599] extract types for config::v1::Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed May 24, 2024
1 parent 8e53982 commit ec1d39a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
16 changes: 12 additions & 4 deletions src/bootstrap/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) {
pub fn setup(log_level: &Option<LogLevel>) {
let level = config_level_or_default(log_level);

if level == log::LevelFilter::Off {
Expand All @@ -25,10 +26,17 @@ pub fn setup(log_level: &Option<String>) {
});
}

fn config_level_or_default(log_level: &Option<String>) -> LevelFilter {
fn config_level_or_default(log_level: &Option<LogLevel>) -> 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,
},
}
}

Expand Down
19 changes: 18 additions & 1 deletion src/config/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
pub log_level: Option<LogLevel>,
/// The website customizable values.
pub website: Website,
/// The tracker configuration.
Expand Down Expand Up @@ -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,
}
3 changes: 2 additions & 1 deletion tests/environments/isolated.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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()
};

Expand Down

0 comments on commit ec1d39a

Please sign in to comment.