feat(zlog): add tests

This commit is contained in:
lily 2025-04-19 16:07:04 -04:00
parent 778776e807
commit 786ff95853

View file

@ -1,5 +1,7 @@
use pretty_assertions::assert_eq;
use tracing::Level;
use serde_json::Map;
use serde::{Serialize, Deserialize};
use super::*;
@ -90,3 +92,52 @@ fn test_logger_sequential_consistency() {
counts.dedup();
assert_eq!(counts.len(), 4096 * 128, "Found duplicate log entries");
}
struct LogJsonStructure {
}
#[test]
fn test_logger_sequential_consistency_json() {
use std::sync::atomic::{AtomicUsize, Ordering};
use tracing::{debug, error, info, trace, warn};
let config = LoggerConfig::default()
.log_to_stdout(false)
.log_to_file(false)
.log_use_json(true)
.log_json_show_timestamp(true)
.log_json_show_level(true)
.log_json_show_message(true)
.log_json_show_additional_fields(false); // Not implemented yet
let logger = Logger::new(config);
static COUNTER: AtomicUsize = AtomicUsize::new(0);
for i in 0..4096 * 128 {
let count = COUNTER.fetch_add(1, Ordering::SeqCst);
match i % 5 {
0 => error!("Error message {}", count),
1 => warn!("Warning message {}", count),
2 => info!("Info message {}", count),
3 => debug!("Debug message {}", count),
_ => trace!("Trace message {}", count),
}
}
let mut log_json: Vec<Map<String, Value>> = vec![];
for log in logger.get_logs(LogQuery::All) {
let mut json_object = serde_json::Map::new();
json_object.insert("timestamp".to_string(), Value::String(DateTime::<Utc>::from(log.timestamp).to_rfc3339()));
json_object.insert("level".to_string(), Value::String(log.level.to_string()));
json_object.insert("message".to_string(), Value::String(log.message.to_string()));
log_json.push(json_object);
}
for log in log_json {
serde_json::to_string(&log).unwrap();
}
}