use log::{debug, error, info, trace, warn};
use log_init::{layout::Text, level_color};
use logforth::{Layout, record::{Level, Record}};
use std::env;
#[static_init::constructor(0)]
extern "C" fn _log_init() {
log_init::init();
}
#[test]
fn test_basic_logging() {
info!("> test {}", 123456);
warn!("This is a warning message");
error!("This is an error message");
debug!("This is a debug message");
trace!("This is a trace message");
}
#[test]
fn test_logging_with_key_values() {
info!(user_id = 42, action = "login"; "User logged in successfully");
warn!(retry_count = 3, timeout = 5000; "Operation timeout, retrying");
error!(error_code = 500, message = "Internal server error"; "Request failed");
}
#[test]
fn test_text_layout_default() {
let layout = Text::default();
assert!(layout.color || !layout.color); }
#[test]
fn test_text_layout_format() {
let layout = Text { color: false };
let record = Record::builder()
.level(Level::Info)
.target("test_target")
.file(Some("test.rs"))
.line(Some(42))
.payload("Test message")
.build();
let result = layout.format(&record, &[]);
assert!(result.is_ok());
let bytes = result.unwrap();
let output = String::from_utf8_lossy(&bytes);
assert!(output.contains("INFO"));
assert!(output.contains("test.rs:42"));
assert!(output.contains("Test message"));
}
#[test]
fn test_level_color_function() {
let error_colored = level_color(Level::Error);
let warn_colored = level_color(Level::Warn);
let info_colored = level_color(Level::Info);
let debug_colored = level_color(Level::Debug);
let trace_colored = level_color(Level::Trace);
assert!(!error_colored.to_string().is_empty());
assert!(!warn_colored.to_string().is_empty());
assert!(!info_colored.to_string().is_empty());
assert!(!debug_colored.to_string().is_empty());
assert!(!trace_colored.to_string().is_empty());
}
#[test]
fn test_timezone_static() {
let tz = &*log_init::TZ;
let tz_str = format!("{tz:?}");
assert!(!tz_str.is_empty());
}
#[test]
fn test_multiple_init_calls() {
let result = std::panic::catch_unwind(|| {
log_init::init();
});
if result.is_err() {
info!("Logger already initialized, which is expected in test environment");
} else {
info!("Logger initialized successfully");
}
}
#[test]
fn test_env_filter() {
unsafe {
env::set_var("RUST_LOG", "debug");
}
debug!("This debug message should be visible");
trace!("This trace message might not be visible depending on filter");
unsafe {
env::remove_var("RUST_LOG");
}
}
#[cfg(target_os = "linux")]
#[test]
fn test_systemd_detection() {
let original = env::var("INVOCATION_ID");
unsafe {
env::remove_var("INVOCATION_ID");
}
log_init::init();
info!("Testing without systemd environment");
unsafe {
env::set_var("INVOCATION_ID", "test-invocation-id");
}
log_init::init();
info!("Testing with systemd environment");
unsafe {
match original {
Ok(val) => env::set_var("INVOCATION_ID", val),
Err(_) => env::remove_var("INVOCATION_ID"),
}
}
}
#[test]
fn test_structured_logging() {
info!(
request_id = "req-123",
user_id = 456,
duration_ms = 250.5,
success = true;
"Request completed"
);
error!(
error_type = "ValidationError",
field = "email",
value = "invalid-email";
"Validation failed"
);
}