Skip to content

Commit

Permalink
Merge #658: Configuration overhaul: print the final configuration to …
Browse files Browse the repository at this point in the history
…console

a2b8fc5 refactor: normalize log texts (Jose Celano)
f39f5e5 feat: print the final configuration to console (Jose Celano)

Pull request description:

  When the app is started, it prints the final configuration to the console in JSON.

  ![image](https://github.com/torrust/torrust-index/assets/58816/0db3553e-755e-40b9-b32c-74f37afb975b)

  **NOTICE:** Secrets are hidden.

ACKs for top commit:
  josecelano:
    ACK a2b8fc5

Tree-SHA512: b35bcc15a6a54f0edc12869ee956b9cfa454a8c3441f8836c4bd52e4aba691519c9b313dafd38d59dfe45102d0402726cc9efbab88522fb9bb3266e37ea98f82
  • Loading branch information
josecelano committed Jul 2, 2024
2 parents bfd3f37 + a2b8fc5 commit 5e8692b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::net::SocketAddr;
use std::sync::Arc;

use tokio::task::JoinHandle;
use tracing::info;

use crate::bootstrap::logging;
use crate::cache::image::manager::ImageCacheService;
Expand Down Expand Up @@ -42,6 +43,8 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running

logging::setup(&log_level);

log_configuration(&configuration).await;

let configuration = Arc::new(configuration);

// Get configuration settings needed to build the app dependencies and
Expand Down Expand Up @@ -190,3 +193,10 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running
tracker_data_importer_handle: tracker_statistics_importer_handle,
}
}

/// It logs the final configuration removing secrets.
async fn log_configuration(configuration: &Configuration) {
let mut setting = configuration.get_all().await.clone();
setting.remove_secrets();
info!("Configuration:\n{}", setting.to_json());
}
2 changes: 1 addition & 1 deletion src/bootstrap/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn tracing_stdout_init(filter: LevelFilter, style: &TraceStyle) {
TraceStyle::Json => builder.json().init(),
};

info!("logging initialized.");
info!("Logging initialized");
}

#[derive(Debug)]
Expand Down
6 changes: 3 additions & 3 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,17 @@ impl Info {
let env_var_config_toml_path = ENV_VAR_CONFIG_TOML_PATH.to_string();

let config_toml = if let Ok(config_toml) = env::var(env_var_config_toml) {
println!("Loading configuration from environment variable {config_toml} ...");
println!("Loading extra configuration from environment variable {config_toml} ...");
Some(config_toml)
} else {
None
};

let config_toml_path = if let Ok(config_toml_path) = env::var(env_var_config_toml_path) {
println!("Loading configuration from file: `{config_toml_path}` ...");
println!("Loading extra configuration from file: `{config_toml_path}` ...");
config_toml_path
} else {
println!("Loading configuration from default configuration file: `{default_config_toml_path}` ...");
println!("Loading extra configuration from default configuration file: `{default_config_toml_path}` ...");
default_config_toml_path
};

Expand Down
20 changes: 20 additions & 0 deletions src/config/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ impl Settings {
"***".clone_into(&mut self.mail.smtp.credentials.password);
self.auth.secret_key = SecretKey::new("***");
}

/// Encodes the configuration to TOML.
///
/// # Panics
///
/// Will panic if it can't be converted to TOML.
#[must_use]
pub fn to_toml(&self) -> String {
toml::to_string(self).expect("Could not encode TOML value")
}

/// Encodes the configuration to JSON.
///
/// # Panics
///
/// Will panic if it can't be converted to JSON.
#[must_use]
pub fn to_json(&self) -> String {
serde_json::to_string_pretty(self).expect("Could not encode JSON value")
}
}

impl Validator for Settings {
Expand Down
2 changes: 1 addition & 1 deletion src/console/commands/seeder/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ use tracing::level_filters::LevelFilter;
pub fn setup(level: LevelFilter) {
tracing_subscriber::fmt().with_max_level(level).init();

debug!("logging initialized.");
debug!("Logging initialized");
}

0 comments on commit 5e8692b

Please sign in to comment.