diff --git a/src/formatter.rs b/src/formatter.rs index ae8e1e5..11a8207 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -250,12 +250,10 @@ mod tests { sync::{Arc, Mutex}, }; - use crate::builder; - use super::*; use chrono::Utc; use tracing::{info, info_span}; - use tracing_subscriber::fmt::{MakeWriter, SubscriberBuilder}; + use tracing_subscriber::fmt::MakeWriter; #[derive(Clone, Debug)] struct MockWriter { @@ -303,8 +301,30 @@ mod tests { } } - fn subscriber() -> SubscriberBuilder { - builder().subscriber_builder() + fn subscriber( + make_writer: MockMakeWriter, + json_event_formatter: Option, + fields_formatter: Option, + ) -> tracing_subscriber::FmtSubscriber< + FieldsFormatter, + JsonEventFormatter, + tracing_core::LevelFilter, + MockMakeWriter, + > { + let json_event_formatter = json_event_formatter.unwrap_or(JsonEventFormatter::default()); + let field_formatter = fields_formatter.unwrap_or(FieldsFormatter::default()); + + tracing_subscriber::fmt::Subscriber::builder() + .event_format(json_event_formatter) + .fmt_fields(field_formatter) + .with_writer(make_writer) + .finish() + } + + fn get_json_object(content: &str) -> serde_json::Value { + let obj: Option = serde_json::from_str(content).ok(); + assert!(matches!(&obj, Some(serde_json::Value::Object(_)))); + obj.expect("matched object") } #[test] @@ -312,19 +332,14 @@ mod tests { use tracing::subscriber; let mock_writer = MockMakeWriter::new(); - let subscriber = subscriber().with_writer(mock_writer.clone()).finish(); + let subscriber = subscriber(mock_writer.clone(), None, None); subscriber::with_default(subscriber, || { info!(life = 42, "Hello, world!"); }); - let content = mock_writer.get_content(); + let obj = get_json_object(&mock_writer.get_content()); - println!("{:?}", content); - - let obj: Option = serde_json::from_str(&content).ok(); - assert!(matches!(obj, Some(serde_json::Value::Object(_)))); - let obj = obj.expect("matched object"); assert_eq!( obj.get("level").unwrap(), &serde_json::Value::String("info".to_owned()) @@ -357,7 +372,7 @@ mod tests { use tracing::subscriber; let mock_writer = MockMakeWriter::new(); - let subscriber = subscriber().with_writer(mock_writer.clone()).finish(); + let subscriber = subscriber(mock_writer.clone(), None, None); subscriber::with_default(subscriber, || { let span = info_span!("hello", "request.uri" = "https://example.com"); @@ -366,13 +381,8 @@ mod tests { }); }); - let content = mock_writer.get_content(); - - println!("{:?}", content); + let obj = get_json_object(&mock_writer.get_content()); - let obj: Option = serde_json::from_str(&content).ok(); - assert!(matches!(obj, Some(serde_json::Value::Object(_)))); - let obj = obj.expect("matched object"); assert_eq!( obj.get("level").unwrap(), &serde_json::Value::String("info".to_owned()) @@ -399,4 +409,52 @@ mod tests { .unwrap(); assert!(timestamp > Utc::now() - chrono::Duration::seconds(1)); } + + #[test] + fn test_all_options() { + use tracing::subscriber; + + let mock_writer = MockMakeWriter::new(); + let subscriber = subscriber( + mock_writer.clone(), + Some( + JsonEventFormatter::default() + .with_level_name("severity") + .with_target_name("logger") + .with_message_name("msg") + .with_timestamp_name("ts") + .with_timestamp_format(crate::TimestampFormat::Unix) + .with_level_value_casing(crate::Casing::Uppercase), + ), + None, + ); + + subscriber::with_default(subscriber, || { + let span = info_span!("hello", "request.uri" = "https://example.com"); + span.in_scope(|| { + info!("Hello, world!"); + }); + }); + + let obj = get_json_object(&mock_writer.get_content()); + + assert_eq!( + obj.get("severity").unwrap(), + &serde_json::Value::String("INFO".to_owned()) + ); + assert_eq!( + obj.get("msg").unwrap(), + &serde_json::Value::String("Hello, world!".to_owned()) + ); + assert_eq!( + obj.get("logger").unwrap(), + &serde_json::Value::String("tracing_ndjson::formatter::tests".to_owned()) + ); + assert_eq!( + obj.get("request.uri").unwrap(), + &serde_json::Value::String("https://example.com".to_owned()) + ); + let timestamp = obj.get("ts").unwrap().as_i64().unwrap(); + assert!(timestamp > Utc::now().timestamp() - 1); + } } diff --git a/src/visitor.rs b/src/visitor.rs index 715c1e5..803c1f1 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -101,3 +101,24 @@ where } } } + +#[cfg(test)] +mod tests { + use serde::{ser::SerializeMap, Serializer}; + + #[test] + fn test_overwrite_message_name() { + use super::Visitor; + + let mut binding = serde_json::Serializer::new(Vec::new()); + let mut serializer = binding.serialize_map(None).unwrap(); + let mut visitor = Visitor::new(&mut serializer, Some("msg")); + + let _ = visitor.serialize_entry("message", "hello"); + visitor.finish().unwrap(); + serializer.end().unwrap(); + + let result = String::from_utf8(binding.into_inner()).unwrap(); + assert_eq!(result, r#"{"msg":"hello"}"#); + } +}