82 lines
2.6 KiB
Rust
82 lines
2.6 KiB
Rust
![]() |
use dir_odt_to_pdf::error::{LogError, ProcessError, Result};
|
||
|
use dir_odt_to_pdf::logging::{LogConfig, init_logging};
|
||
|
use log::{LevelFilter, debug, info};
|
||
|
use std::fs;
|
||
|
|
||
|
#[test]
|
||
|
fn test_log_level_changes() -> Result<()> {
|
||
|
// Create a temporary file for logging
|
||
|
let temp_dir = tempfile::TempDir::new().unwrap();
|
||
|
let log_path = temp_dir.path().join("test.log");
|
||
|
|
||
|
// Initialize logging with Info level first
|
||
|
let config = LogConfig {
|
||
|
log_file: Some(log_path.clone()),
|
||
|
log_level: LevelFilter::Info,
|
||
|
append_log: false,
|
||
|
};
|
||
|
|
||
|
// Initialize logging
|
||
|
init_logging(config)?;
|
||
|
|
||
|
// Write some messages at different levels
|
||
|
debug!("First debug message"); // Should not appear (we're at Info level)
|
||
|
info!("First info message"); // Should appear
|
||
|
|
||
|
// Read the log file
|
||
|
let content = fs::read_to_string(&log_path).map_err(ProcessError::Io)?;
|
||
|
println!("=== Initial Log Contents ===");
|
||
|
println!("{}", content);
|
||
|
println!("=======================");
|
||
|
|
||
|
// Verify initial messages
|
||
|
assert!(
|
||
|
!content.contains("First debug message"),
|
||
|
"Debug message should not appear at Info level"
|
||
|
);
|
||
|
assert!(
|
||
|
content.contains("First info message"),
|
||
|
"Info message should appear"
|
||
|
);
|
||
|
|
||
|
// Try to change log level - this should fail with AlreadyInitialized
|
||
|
let config = LogConfig {
|
||
|
log_file: Some(log_path.clone()),
|
||
|
log_level: LevelFilter::Debug,
|
||
|
append_log: true,
|
||
|
};
|
||
|
|
||
|
match init_logging(config) {
|
||
|
Err(ProcessError::Log(LogError::AlreadyInitialized)) => {
|
||
|
// Expected error
|
||
|
info!("Got expected AlreadyInitialized error");
|
||
|
|
||
|
// Write more messages
|
||
|
debug!("Second debug message"); // Still won't appear (still at Info level)
|
||
|
info!("Second info message"); // Will appear
|
||
|
|
||
|
// Check final log contents
|
||
|
let content = fs::read_to_string(&log_path).map_err(ProcessError::Io)?;
|
||
|
println!("\n=== Final Log Contents ===");
|
||
|
println!("{}", content);
|
||
|
println!("=======================");
|
||
|
|
||
|
// Verify final state
|
||
|
assert!(
|
||
|
!content.contains("Second debug message"),
|
||
|
"Debug messages should still not appear"
|
||
|
);
|
||
|
assert!(
|
||
|
content.contains("Second info message"),
|
||
|
"Info messages should still appear"
|
||
|
);
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
|
Ok(_) => {
|
||
|
panic!("Expected AlreadyInitialized error, but logger was reinitialized")
|
||
|
}
|
||
|
Err(e) => Err(e),
|
||
|
}
|
||
|
}
|