diff --git a/src/formatter.rs b/src/formatter.rs index d93183c..f527f28 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -39,6 +39,8 @@ pub struct EventsFormatter { pub(crate) with_target: bool, pub(crate) with_span_name: bool, pub(crate) with_span_path: bool, + pub(crate) with_location: bool, + pub(crate) with_module_path: bool, } impl Default for EventsFormatter { @@ -48,6 +50,8 @@ impl Default for EventsFormatter { with_target: true, with_span_name: true, with_span_path: true, + with_location: false, + with_module_path: false, } } } @@ -118,6 +122,17 @@ where None }; + if self.with_location { + if let (Some(file), Some(line)) = (metadata.file(), metadata.line()) { + serializer.serialize_entry("location", &format!("{}:{}", file, line))?; + } + } + if self.with_module_path { + if let Some(module) = metadata.module_path() { + serializer.serialize_entry("module_path", module)?; + } + } + if let Some(span) = span { if self.with_span_name { serializer.serialize_entry("span", span.name())?; @@ -531,6 +546,60 @@ mod tests { assert!(content.contains(&message)); } + #[test] + #[cfg(not(feature = "ansi_logs"))] + fn test_enable_location() { + use tracing::subscriber; + + let mock_writer = MockMakeWriter::new(); + let subscriber = builder::builder() + .with_location(true) + .subscriber_builder() + .with_writer(mock_writer.clone()) + .finish(); + + subscriber::with_default(subscriber, || { + let _top = info_span!("top").entered(); + let _middle = info_span!("middle").entered(); + let _bottom = info_span!("bottom").entered(); + + tracing::info!("message"); + }); + + let content = mock_writer.get_content(); + + println!("{}", content); + assert!(content.contains("location=src/formatter.rs:566")); + assert!(content.contains("info")); + } + + #[test] + #[cfg(not(feature = "ansi_logs"))] + fn test_enable_module_path() { + use tracing::subscriber; + + let mock_writer = MockMakeWriter::new(); + let subscriber = builder::builder() + .with_module_path(true) + .subscriber_builder() + .with_writer(mock_writer.clone()) + .finish(); + + subscriber::with_default(subscriber, || { + let _top = info_span!("top").entered(); + let _middle = info_span!("middle").entered(); + let _bottom = info_span!("bottom").entered(); + + tracing::info!("message"); + }); + + let content = mock_writer.get_content(); + + println!("{}", content); + assert!(content.contains("module_path=tracing_logfmt::formatter::tests")); + assert!(content.contains("info")); + } + #[cfg(feature = "ansi_logs")] fn make_ansi_key_value(key: &str, value: &str) -> String { use nu_ansi_term::Color; diff --git a/src/formatter/builder.rs b/src/formatter/builder.rs index 6e5f049..fe036dd 100644 --- a/src/formatter/builder.rs +++ b/src/formatter/builder.rs @@ -53,6 +53,14 @@ impl Builder { self.events.with_span_path = enable; self } + pub fn with_location(mut self, enable: bool) -> Self { + self.events.with_location = enable; + self + } + pub fn with_module_path(mut self, enable: bool) -> Self { + self.events.with_module_path = enable; + self + } pub fn layer(self) -> Layer where