From 280603687b66b14a6bc2bf45b5a50fad1d04880c Mon Sep 17 00:00:00 2001 From: Cole MacKenzie Date: Sun, 8 Oct 2023 10:53:51 -0700 Subject: [PATCH] fix: try to capture span fields --- src/formatter.rs | 7 +------ src/lib.rs | 54 ++++++++++++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/formatter.rs b/src/formatter.rs index dae38a1..22877f5 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -129,13 +129,8 @@ where .map_err(Error::Serde)?; }; - let span = event - .parent() - .and_then(|id| ctx.span(id)) - .or_else(|| ctx.lookup_current()); - // Write all fields from spans - if let Some(leaf_span) = span { + if let Some(leaf_span) = ctx.lookup_current() { for span in leaf_span.scope().from_root() { let ext = span.extensions(); let data = ext diff --git a/src/lib.rs b/src/lib.rs index 53893c0..2a7916d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -208,8 +208,8 @@ impl Builder { S: Subscriber + for<'a> LookupSpan<'a>, { tracing_subscriber::fmt::layer() - .event_format(self.events) .fmt_fields(self.fields) + .event_format(self.events) } pub fn subscriber_builder( @@ -234,17 +234,13 @@ where mod tests { use super::*; + use tracing::{debug, error, info, info_span, trace, warn}; - use tracing_core::Level; + use tracing_subscriber::prelude::*; #[test] fn test_json_event_formatter() { - let formatter = formatter::JsonEventFormatter::new(); - let subscriber = tracing_subscriber::fmt() - .fmt_fields(formatter::FieldsFormatter::new()) - .event_format(formatter) - .with_max_level(Level::TRACE) - .finish(); + let subscriber = tracing_subscriber::registry().with(builder().layer()); tracing::subscriber::with_default(subscriber, || { trace!(a = "b", "hello world from trace"); @@ -252,24 +248,34 @@ mod tests { info!("hello world from info"); warn!("hello world from warn"); error!("hello world from error"); - - let span = info_span!("test_span", b = "b", d = "d", later = tracing::field::Empty,); + let span = info_span!( + "test_span", + person.firstname = "cole", + person.lastname = "mackenzie", + later = tracing::field::Empty, + ); span.in_scope(|| { info!("some message from inside a info_span"); + let inner = info_span!("inner_span", a = "b", c = "d", inner_span = true); + inner.in_scope(|| { + info!( + inner_span_field = true, + later = "populated from inside a span", + "some message from inside a info_span", + ); + }); }); }); - let formatter = formatter::JsonEventFormatter::new() - .with_level_name("severity") - .with_message_name("msg") - .with_timestamp_name("ts") - .with_timestamp_format(TimestampFormat::Unix); + let subscriber = tracing_subscriber::registry().with( + builder() + .with_level_name("severity") + .with_message_name("message") + .with_timestamp_name("ts") + .with_timestamp_format(TimestampFormat::Unix) + .layer(), + ); - let subscriber = tracing_subscriber::fmt() - .fmt_fields(formatter::FieldsFormatter::new()) - .event_format(formatter) - .with_max_level(Level::TRACE) - .finish(); tracing::subscriber::with_default(subscriber, || { trace!(a = "b", "hello world from trace"); debug!("hello world from debug"); @@ -284,9 +290,13 @@ mod tests { ); span.in_scope(|| { info!("some message from inside a info_span"); - let inner = info_span!("inner_span", a = "b", c = "d"); + let inner = info_span!("inner_span", a = "b", c = "d", inner_span = true); inner.in_scope(|| { - info!("some message from inside a info_span"); + info!( + inner_span_field = true, + later = "populated from inside a span", + "some message from inside a info_span", + ); }); }); });