From b9637e7cae16d940c274b3045550f609364d8680 Mon Sep 17 00:00:00 2001 From: Stefano Vittorio Porta Date: Sun, 26 Nov 2023 01:18:46 +0100 Subject: [PATCH] feat: added logging directory settings --- config.yaml | 4 +++- src/config.rs | 8 +++++++- src/main.rs | 40 +++++++++++++++++++++++++--------------- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/config.yaml b/config.yaml index 83acd02..925f0ae 100644 --- a/config.yaml +++ b/config.yaml @@ -1,4 +1,6 @@ -log_level: INFO +logging: + log_level: INFO + log_directory: ./logs database: username: postgres diff --git a/src/config.rs b/src/config.rs index f68ebbe..c62627a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,7 +8,13 @@ pub struct Config { pub database: DbConfig, pub listen_port: Option, pub remote_address: Option, - pub logging_level: Option, + pub logging: Option, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct Logging { + pub level: Option, + pub directory: Option, } #[derive(Serialize, Deserialize, Debug)] diff --git a/src/main.rs b/src/main.rs index 2656301..6af5963 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,17 +62,14 @@ async fn main() -> Result<()> { let config_path: &String = args.get_one("config_path").unwrap(); - let config = config::load_from_yaml(config_path) - .await - .context("Failed to load the configuration file")?; + let config = config::load_from_yaml(config_path).await.context(format!( + "Failed to load the configuration file from {}", + config_path + ))?; // Set up logging - let _logger_guard = init_logging(&config); - // Hook to log also panics with tracing - std::panic::set_hook(Box::new(panic_hook)); - // Finally starting! info!("{} version {} started.", crate_name!(), crate_version!()); @@ -155,25 +152,38 @@ async fn main() -> Result<()> { } fn init_logging(config: &Config) -> WorkerGuard { - let base_logging = config.logging_level.clone().unwrap_or("info".to_string()); - let base_logging = LevelFilter::from_str(&base_logging).unwrap(); + let options = config.logging.as_ref(); - let filter = EnvFilter::builder() + let base_logging = LevelFilter::from_str( + &options + .and_then(|logging| logging.level.clone()) + .unwrap_or("info".to_string()), + ) + .unwrap(); + + let console_logging_filter = EnvFilter::builder() .with_default_directive(base_logging.into()) .with_env_var("LOG_LEVEL") .from_env_lossy(); - let file_appender = tracing_appender::rolling::daily("./logs", "growatt_server"); - let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender); + let file_appender = tracing_appender::rolling::daily( + options + .and_then(|l| l.directory.clone()) + .unwrap_or("./logs".to_string()), + "growatt_server", + ); + let (non_blocking, guard) = tracing_appender::non_blocking(file_appender); tracing_subscriber::registry() .with(fmt::layer()) - // .with(filter) - .with(filter) + .with(console_logging_filter) .with(fmt::layer().with_writer(non_blocking)) .init(); - _guard + // Hook to log also panics with tracing + std::panic::set_hook(Box::new(panic_hook)); + + guard } struct ConnectionHandler {