Skip to content

Commit

Permalink
feat: add formatter options for location (file + line number) and mod…
Browse files Browse the repository at this point in the history
…ule_path
  • Loading branch information
zeeshanlakhani committed Feb 28, 2024
1 parent 855d74a commit 5fa66ce
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -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())?;
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/formatter/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S>(self) -> Layer<S, FieldsFormatter, EventsFormatter>
where
Expand Down

0 comments on commit 5fa66ce

Please sign in to comment.