Skip to content

Commit

Permalink
feat: [torrust#591] move log_level into a new logging section in config
Browse files Browse the repository at this point in the history
Old:

```toml
log_level = "info"

[website]
name = "Torrust"
```

New:

```toml
[logging]
log_level = "info"

[website]
name = "Torrust"
```

And the value is not Optional anymore. It was a Optional<LogLevel> but
when it was None it defaulted to LogLevel::Info. In practice that means
is mandatory but with the `Info` default value.
  • Loading branch information
josecelano committed Jun 12, 2024
1 parent 12010f1 commit 49c964b
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 47 deletions.
1 change: 1 addition & 0 deletions share/default/config/index.container.mysql.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[logging]
log_level = "info"

[database]
Expand Down
1 change: 1 addition & 0 deletions share/default/config/index.container.sqlite3.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[logging]
log_level = "info"

[database]
Expand Down
1 change: 1 addition & 0 deletions share/default/config/index.development.sqlite3.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[logging]
log_level = "info"

# Uncomment if you want to enable TSL for development
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[logging]
log_level = "info"

[tracker]
Expand Down
1 change: 1 addition & 0 deletions share/default/config/index.public.e2e.container.mysql.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[logging]
log_level = "info"

[tracker]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[logging]
log_level = "info"

[tracker]
Expand Down
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct Running {
/// It panics if there is an error connecting to the database.
#[allow(clippy::too_many_lines)]
pub async fn run(configuration: Configuration, api_version: &Version) -> Running {
let log_level = configuration.settings.read().await.log_level.clone();
let log_level = configuration.settings.read().await.logging.log_level.clone();

logging::setup(&log_level);

Expand Down
20 changes: 3 additions & 17 deletions src/bootstrap/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use std::sync::Once;
use tracing::info;
use tracing::level_filters::LevelFilter;

use crate::config::v1::LogLevel;
use crate::config::v1::logging::LogLevel;

static INIT: Once = Once::new();

pub fn setup(log_level: &Option<LogLevel>) {
let tracing_level = config_level_or_default(log_level);
pub fn setup(log_level: &LogLevel) {
let tracing_level: LevelFilter = log_level.clone().into();

if tracing_level == LevelFilter::OFF {
return;
Expand All @@ -27,20 +27,6 @@ pub fn setup(log_level: &Option<LogLevel>) {
});
}

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

fn tracing_stdout_init(filter: LevelFilter, style: &TraceStyle) {
let builder = tracing_subscriber::fmt().with_max_level(filter);

Expand Down
6 changes: 5 additions & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub type Mail = v1::mail::Mail;
pub type Network = v1::net::Network;
pub type TrackerStatisticsImporter = v1::tracker_statistics_importer::TrackerStatisticsImporter;
pub type Tracker = v1::tracker::Tracker;
pub type Logging = v1::logging::Logging;
pub type Website = v1::website::Website;
pub type EmailOnSignup = v1::auth::EmailOnSignup;

Expand Down Expand Up @@ -325,7 +326,10 @@ mod tests {

#[cfg(test)]
fn default_config_toml() -> String {
let config = r#"[website]
let config = r#"[logging]
log_level = "info"
[website]
name = "Torrust"
[tracker]
Expand Down
76 changes: 76 additions & 0 deletions src/config/v1/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use std::fmt;

use serde::{Deserialize, Serialize};
use tracing::level_filters::LevelFilter;

/// Core configuration for the API
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Logging {
/// Logging level. Possible values are: `Off`, `Error`, `Warn`, `Info`, `Debug`, `Trace`.
#[serde(default = "Logging::default_log_level")]
pub log_level: LogLevel,
}

impl Default for Logging {
fn default() -> Self {
Self {
log_level: Logging::default_log_level(),
}
}
}

impl Logging {
fn default_log_level() -> LogLevel {
LogLevel::Info
}
}

#[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,
}

impl Default for LogLevel {
fn default() -> Self {
Self::Info
}
}

impl fmt::Display for LogLevel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let display_str = match self {
LogLevel::Off => "off",
LogLevel::Error => "error",
LogLevel::Warn => "warn",
LogLevel::Info => "info",
LogLevel::Debug => "debug",
LogLevel::Trace => "trace",
};
write!(f, "{display_str}")
}
}

impl From<LogLevel> for LevelFilter {
fn from(log_level: LogLevel) -> Self {
match log_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,
}
}
}
24 changes: 4 additions & 20 deletions src/config/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ pub mod api;
pub mod auth;
pub mod database;
pub mod image_cache;
pub mod logging;
pub mod mail;
pub mod net;
pub mod tracker;
pub mod tracker_statistics_importer;
pub mod website;

use logging::Logging;
use serde::{Deserialize, Serialize};

use self::api::Api;
Expand All @@ -24,10 +26,9 @@ use super::validator::{ValidationError, Validator};
/// The whole configuration for the index.
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)]
pub struct Settings {
/// Logging level. Possible values are: `Off`, `Error`, `Warn`, `Info`,
/// `Debug` and `Trace`. Default is `Info`.
/// The logging configuration.
#[serde(default)]
pub log_level: Option<LogLevel>,
pub logging: Logging,
/// The website customizable values.
#[serde(default)]
pub website: Website,
Expand Down Expand Up @@ -73,20 +74,3 @@ 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,
}
2 changes: 1 addition & 1 deletion src/console/commands/tracker_statistics_importer/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub async fn import() {

let configuration = initialize_configuration();

let log_level = configuration.settings.read().await.log_level.clone();
let log_level = configuration.settings.read().await.logging.log_level.clone();

logging::setup(&log_level);

Expand Down
19 changes: 17 additions & 2 deletions tests/common/contexts/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ pub mod responses;
use serde::{Deserialize, Serialize};
use torrust_index::config::v1::tracker::ApiToken;
use torrust_index::config::{
Api as DomainApi, Auth as DomainAuth, Database as DomainDatabase, ImageCache as DomainImageCache, Mail as DomainMail,
Network as DomainNetwork, Settings as DomainSettings, Tracker as DomainTracker,
Api as DomainApi, Auth as DomainAuth, Database as DomainDatabase, ImageCache as DomainImageCache, Logging as DomainLogging,
Mail as DomainMail, Network as DomainNetwork, Settings as DomainSettings, Tracker as DomainTracker,
TrackerStatisticsImporter as DomainTrackerStatisticsImporter, Website as DomainWebsite,
};
use url::Url;

#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
pub struct Settings {
pub logging: Logging,
pub website: Website,
pub tracker: Tracker,
pub net: Network,
Expand All @@ -22,6 +23,11 @@ pub struct Settings {
pub tracker_statistics_importer: TrackerStatisticsImporter,
}

#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
pub struct Logging {
pub log_level: String,
}

#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
pub struct Website {
pub name: String,
Expand Down Expand Up @@ -90,6 +96,7 @@ pub struct TrackerStatisticsImporter {
impl From<DomainSettings> for Settings {
fn from(settings: DomainSettings) -> Self {
Settings {
logging: Logging::from(settings.logging),
website: Website::from(settings.website),
tracker: Tracker::from(settings.tracker),
net: Network::from(settings.net),
Expand All @@ -103,6 +110,14 @@ impl From<DomainSettings> for Settings {
}
}

impl From<DomainLogging> for Logging {
fn from(logging: DomainLogging) -> Self {
Self {
log_level: logging.log_level.to_string(),
}
}
}

impl From<DomainWebsite> for Website {
fn from(website: DomainWebsite) -> Self {
Self { name: website.name }
Expand Down
9 changes: 4 additions & 5 deletions tests/environments/isolated.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use tempfile::TempDir;
use torrust_index::config;
use torrust_index::config::v1::LogLevel;
use torrust_index::config::v1::logging::LogLevel;
use torrust_index::config::FREE_PORT;
use torrust_index::web::api::Version;
use url::Url;
Expand Down Expand Up @@ -72,10 +72,9 @@ 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(LogLevel::Off), // Change to `debug` for tests debugging
..config::Settings::default()
};
let mut configuration = config::Settings::default();

configuration.logging.log_level = LogLevel::Off; // Change to `debug` for tests debugging

// Ephemeral API port
configuration.net.port = FREE_PORT;
Expand Down

0 comments on commit 49c964b

Please sign in to comment.