Skip to content

Commit

Permalink
fix: extra debug logging when in testing mode (#310)
Browse files Browse the repository at this point in the history
Co-authored-by: Erik Bjäreholt <erik@bjareho.lt>
  • Loading branch information
ShootingKing-AM and ErikBjare authored Nov 2, 2022
1 parent 922f9b3 commit 5bd70f9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
9 changes: 6 additions & 3 deletions aw-server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
20 changes: 13 additions & 7 deletions aw-server/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
"[{}][{}][{}]: {}",
Expand All @@ -74,7 +81,6 @@ pub fn setup_logger(testing: bool) -> Result<(), fern::InitError> {
.chain(fern::log_file(logfile_path)?),
)
.apply()?;

Ok(())
}

Expand Down
4 changes: 4 additions & 0 deletions aw-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5bd70f9

Please sign in to comment.