From 5bd70f94a8e400af67796a3e541c74d225720b30 Mon Sep 17 00:00:00 2001 From: Harsha Raghu Date: Wed, 2 Nov 2022 22:15:34 +0530 Subject: [PATCH] fix: extra debug logging when in testing mode (#310) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Erik Bjäreholt --- aw-server/src/config.rs | 9 ++++++--- aw-server/src/logging.rs | 20 +++++++++++++------- aw-server/src/main.rs | 4 ++++ 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/aw-server/src/config.rs b/aw-server/src/config.rs index 0eb11da9..57c78712 100644 --- a/aw-server/src/config.rs +++ b/aw-server/src/config.rs @@ -3,6 +3,7 @@ use std::io::{Read, Write}; use rocket::config::Config; use rocket::data::{Limits, ToByteUnit}; +use rocket::log::LogLevel; use serde::{Deserialize, Serialize}; use crate::dirs; @@ -43,10 +44,12 @@ impl Default for AWConfig { impl AWConfig { pub fn to_rocket_config(&self) -> rocket::Config { - let mut config = if self.testing { - Config::release_default() + let mut config; + if self.testing { + config = Config::debug_default(); + config.log_level = LogLevel::Debug; } else { - Config::debug_default() + config = Config::release_default() }; // Needed for bucket imports diff --git a/aw-server/src/logging.rs b/aw-server/src/logging.rs index 465027cc..89648966 100644 --- a/aw-server/src/logging.rs +++ b/aw-server/src/logging.rs @@ -44,12 +44,19 @@ pub fn setup_logger(testing: bool) -> Result<(), fern::InitError> { _ => default_log_level, }; - fern::Dispatch::new() - // Set some Rocket messages to debug level - .level(log_level) - .level_for("rocket", log::LevelFilter::Warn) - .level_for("_", log::LevelFilter::Warn) // Rocket requests - .level_for("launch_", log::LevelFilter::Warn) // Rocket config info + let mut dispatch = fern::Dispatch::new().level(log_level); + // Set some Rocket messages to debug level + + let is_debug = matches!(log_level, log::LevelFilter::Trace | log::LevelFilter::Debug); + if is_debug { + dispatch = dispatch + .level_for("rocket", log::LevelFilter::Warn) + .level_for("_", log::LevelFilter::Warn) // Rocket requests + .level_for("launch_", log::LevelFilter::Warn); // Rocket config info + } + + dispatch + // Formatting .format(move |out, message, record| { out.finish(format_args!( "[{}][{}][{}]: {}", @@ -74,7 +81,6 @@ pub fn setup_logger(testing: bool) -> Result<(), fern::InitError> { .chain(fern::log_file(logfile_path)?), ) .apply()?; - Ok(()) } diff --git a/aw-server/src/main.rs b/aw-server/src/main.rs index 8466a157..975a3024 100644 --- a/aw-server/src/main.rs +++ b/aw-server/src/main.rs @@ -63,6 +63,10 @@ async fn main() -> Result<(), rocket::Error> { logging::setup_logger(testing).expect("Failed to setup logging"); + if testing { + info!("Running server in Testing mode"); + } + let mut config = config::create_config(testing); // set host if overridden