use rsnaker::game_logic::logger::log_configuration::{LogLevel, init_logger, update_log_level};
use std::fs;
use std::path::Path;
use std::thread;
use std::time::Duration;
#[test]
fn test_logger_writes_to_file_and_updates_level() {
let log_file_name = "test_output.log";
let log_path = Path::new(log_file_name);
let config_path = "test_log_config_runtime.toml";
if log_path.exists() {
fs::remove_file(log_path).unwrap();
}
let toml_content = format!(
r#"level = "info"
file_name = "{log_file_name}"
time_format = "[hour]:[minute]:[second]"
with_ansi = false
with_target = false
with_thread_names = true
with_thread_ids = false
with_line_number = true
with_file = true
with_level = true
"#
);
fs::write(config_path, toml_content).unwrap();
{
let _guard = init_logger(Some(config_path));
tracing::info!(target: "rsnaker", "This is an info message");
tracing::debug!(target: "rsnaker", "This is a hidden debug message");
update_log_level(LogLevel::Debug);
tracing::debug!(target: "rsnaker", "This is a visible debug message");
update_log_level(LogLevel::Off);
tracing::error!(target: "rsnaker", "This error should be hidden");
thread::sleep(Duration::from_millis(200));
}
let content = fs::read_to_string(log_path).expect("Failed to read log file");
assert!(
content.contains("This is an info message"),
"Info message missing"
);
assert!(
!content.contains("This is a hidden debug message"),
"Hidden debug message found"
);
assert!(
content.contains("This is a visible debug message"),
"Visible debug message missing"
);
assert!(
!content.contains("This error should be hidden"),
"Hidden error message found"
);
fs::remove_file(log_path).unwrap();
let _ = fs::remove_file(config_path);
}