()
| 273 | |
| 274 | #[test] |
| 275 | fn test_logging() { |
| 276 | // Create a temp path for where we write logs to. |
| 277 | let tempdir = tempfile::TempDir::new().unwrap(); |
| 278 | let log_path = tempdir.path().join("test.log"); |
| 279 | |
| 280 | // Assert that initialize logging simply doesn't panic. |
| 281 | let _guard = initialize_logging(LogArgs { |
| 282 | log_level: Some("trace".to_owned()), |
| 283 | log_to_stdout: true, |
| 284 | log_file_path: Some(&log_path), |
| 285 | delete_old_log_file: true, |
| 286 | }) |
| 287 | .unwrap(); |
| 288 | |
| 289 | // Test that get log level functions as expected. |
| 290 | assert_eq!(get_log_level(), "trace"); |
| 291 | |
| 292 | // Write some log messages out to file. (and stderr) |
| 293 | trace!("abc"); |
| 294 | debug!("def"); |
| 295 | info!("ghi"); |
| 296 | warn!("jkl"); |
| 297 | error!("mno"); |
| 298 | |
| 299 | // Test that set log level functions as expected. |
| 300 | // This also restores the default log level. |
| 301 | set_log_level(DEFAULT_FILTER.to_string()).unwrap(); |
| 302 | assert_eq!(get_log_level(), DEFAULT_FILTER.to_string()); |
| 303 | |
| 304 | // Sleep in order to ensure logs get written to file, then assert on the contents |
| 305 | std::thread::sleep(Duration::from_millis(100)); |
| 306 | let logs = read_to_string(&log_path).unwrap(); |
| 307 | for i in [ |
| 308 | "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "abc", "def", "ghi", "jkl", "mno", |
| 309 | ] { |
| 310 | assert!(logs.contains(i)); |
| 311 | } |
| 312 | } |
| 313 | } |
nothing calls this directly
no test coverage detected