use std::time::Duration;
use log2::*;
const PATH: &str = "tests/log2.txt";
const REDIRECT: &str = "tests/redirect_file.txt";
#[test]
fn redirect_log_file() {
log2::open(PATH).module(true).tee(true).start();
trace!("send order request to server");
debug!("receive order response");
info!("order was executed");
warn!("network speed is slow");
error!("network connection was broken");
if let Some(mut handle) = log2::handle() {
if let Some(h) = handle.as_mut() {
h.redirect(REDIRECT);
}
}
trace!("send order request to server");
debug!("receive order response");
info!("order was executed");
warn!("network speed is slow");
error!("network connection was broken");
if let Some(handle) = log2::handle() {
if let Some(h) = handle.as_ref() {
h.flush();
}
}
std::thread::sleep(Duration::from_millis(1000));
let file_path = std::path::Path::new(REDIRECT);
assert!(file_path.exists(), "{REDIRECT} file was not created");
let log_content = std::fs::read_to_string(file_path).expect("Failed to read the log file");
assert!(
log_content.contains("network connection was broken"),
"Log content does not match"
);
}