Skip to content

Commit

Permalink
Make keys in JsonLayer more configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
mladedav committed Jun 18, 2024
1 parent 048a84c commit 22036d8
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 187 deletions.
57 changes: 46 additions & 11 deletions src/fmt/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ use tracing_subscriber::{
Registry,
};

use super::names::{
CURRENT_SPAN,
FIELDS,
FILENAME,
LEVEL,
LINE_NUMBER,
SPAN_LIST,
TARGET,
THREAD_ID,
THREAD_NAME,
TIMESTAMP,
};
use crate::layer::JsonLayer;

/// Configures and constructs `Subscriber`s.
Expand Down Expand Up @@ -142,19 +154,42 @@ where
let mut layer = JsonLayer::<S>::new(self.make_writer);

if self.display_timestamp {
layer.with_timer(self.timer);
layer.with_timer(TIMESTAMP, self.timer);
}

if self.display_level {
layer.with_level(LEVEL);
}

if self.display_target {
layer.with_target(TARGET);
}

if self.display_filename {
layer.with_file(FILENAME);
}

if self.display_line_number {
layer.with_line_number(LINE_NUMBER);
}

layer
.with_level(self.display_level)
.flatten_event(self.flatten_event)
.with_target(self.display_target)
.with_file(self.display_filename)
.with_line_number(self.display_line_number)
.with_current_span(self.display_current_span)
.with_span_list(self.display_span_list)
.with_thread_names(self.display_thread_name)
.with_thread_ids(self.display_thread_id);
if self.display_thread_name {
layer.with_thread_names(THREAD_NAME);
}

if self.display_thread_id {
layer.with_thread_ids(THREAD_ID);
}

layer.with_event(FIELDS, self.flatten_event);

if self.display_current_span {
layer.with_current_span(CURRENT_SPAN);
}

if self.display_span_list {
layer.with_span_list(SPAN_LIST);
}

(layer, self.filter)
}
Expand Down
83 changes: 64 additions & 19 deletions src/fmt/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ use tracing_subscriber::{
Registry,
};

use super::names::{
CURRENT_SPAN,
FIELDS,
FILENAME,
LEVEL,
LINE_NUMBER,
SPAN_LIST,
TARGET,
THREAD_ID,
THREAD_NAME,
TIMESTAMP,
};
use crate::layer::JsonLayer;

/// A [`Layer`] that logs JSON formatted representations of `tracing` events.
Expand Down Expand Up @@ -60,12 +72,12 @@ impl<S: Subscriber + for<'lookup> LookupSpan<'lookup>> Default for Layer<S> {

inner
// If we do not call this, fields are not printed at all.
.flatten_event(false)
.with_timer(SystemTime)
.with_target(true)
.with_level(true)
.with_current_span(true)
.with_span_list(true);
.with_event(FIELDS, false)
.with_timer(TIMESTAMP, SystemTime)
.with_target(TARGET)
.with_level(LEVEL)
.with_current_span(CURRENT_SPAN)
.with_span_list(SPAN_LIST);

Self { inner }
}
Expand Down Expand Up @@ -329,13 +341,17 @@ where

/// Sets the JSON subscriber being built to flatten event metadata.
pub fn flatten_event(mut self, flatten_event: bool) -> Self {
self.inner.flatten_event(flatten_event);
self.inner.with_event(FIELDS, flatten_event);
self
}

/// Sets whether or not the formatter will include the current span in formatted events.
pub fn with_current_span(mut self, display_current_span: bool) -> Self {
self.inner.with_current_span(display_current_span);
if display_current_span {
self.inner.with_current_span(CURRENT_SPAN);
} else {
self.inner.remove_field(CURRENT_SPAN);
}
self
}

Expand All @@ -344,7 +360,11 @@ where
///
/// This overrides any previous calls to [`with_flat_span_list`](Self::with_flat_span_list).
pub fn with_span_list(mut self, display_span_list: bool) -> Self {
self.inner.with_span_list(display_span_list);
if display_span_list {
self.inner.with_span_list(SPAN_LIST);
} else {
self.inner.remove_field(SPAN_LIST);
}
self
}

Expand All @@ -355,9 +375,9 @@ where
/// This overrides any previous calls to [`with_span_list`](Self::with_span_list).
pub fn with_flat_span_list(mut self, flatten_span_list: bool) -> Self {
if flatten_span_list {
self.inner.flatten_span_list();
self.inner.flatten_span_list(SPAN_LIST);
} else {
self.inner.with_span_list(false);
self.inner.remove_field(SPAN_LIST);
}
self
}
Expand All @@ -377,19 +397,23 @@ where
/// [`LocalTime`]: tracing_subscriber::fmt::time::LocalTime
/// [`time` crate]: https://docs.rs/time/0.3
pub fn with_timer<T: FormatTime + Send + Sync + 'static>(mut self, timer: T) -> Self {
self.inner.with_timer(timer);
self.inner.with_timer(TIMESTAMP, timer);
self
}

/// Do not emit timestamps with log messages.
pub fn without_time(mut self) -> Self {
self.inner.without_time();
self.inner.remove_field(TIMESTAMP);
self
}

/// Sets whether or not an event's target is displayed.
pub fn with_target(mut self, display_target: bool) -> Self {
self.inner.with_target(display_target);
if display_target {
self.inner.with_target(TARGET);
} else {
self.inner.remove_field(TARGET)
}

self
}
Expand All @@ -399,7 +423,11 @@ where
///
/// [file]: tracing_core::Metadata::file
pub fn with_file(mut self, display_filename: bool) -> Self {
self.inner.with_file(display_filename);
if display_filename {
self.inner.with_file(FILENAME);
} else {
self.inner.remove_field(FILENAME);
}
self
}

Expand All @@ -408,13 +436,21 @@ where
///
/// [line]: tracing_core::Metadata::line
pub fn with_line_number(mut self, display_line_number: bool) -> Self {
self.inner.with_line_number(display_line_number);
if display_line_number {
self.inner.with_line_number(LINE_NUMBER);
} else {
self.inner.remove_field(LINE_NUMBER);
}
self
}

/// Sets whether or not an event's level is displayed.
pub fn with_level(mut self, display_level: bool) -> Self {
self.inner.with_level(display_level);
if display_level {
self.inner.with_level(LEVEL);
} else {
self.inner.remove_field(LEVEL);
}
self
}

Expand All @@ -423,7 +459,11 @@ where
///
/// [name]: std::thread#naming-threads
pub fn with_thread_names(mut self, display_thread_name: bool) -> Self {
self.inner.with_thread_names(display_thread_name);
if display_thread_name {
self.inner.with_thread_names(THREAD_NAME);
} else {
self.inner.remove_field(THREAD_NAME);
}
self
}

Expand All @@ -432,7 +472,12 @@ where
///
/// [thread ID]: std::thread::ThreadId
pub fn with_thread_ids(mut self, display_thread_id: bool) -> Self {
self.inner.with_thread_ids(display_thread_id);
if display_thread_id {
self.inner.with_thread_ids(THREAD_ID);
} else {
self.inner.remove_field(THREAD_ID);
}

self
}

Expand Down
1 change: 1 addition & 0 deletions src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use tracing_subscriber::{registry::LookupSpan, util::SubscriberInitExt};

mod builder;
mod layer;
mod names;

/// Returns a new [`SubscriberBuilder`] for configuring a json [formatting subscriber].
///
Expand Down
10 changes: 10 additions & 0 deletions src/fmt/names.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pub(super) const CURRENT_SPAN: &str = "span";
pub(super) const FIELDS: &str = "fields";
pub(super) const FILENAME: &str = "filename";
pub(super) const LEVEL: &str = "level";
pub(super) const LINE_NUMBER: &str = "line_number";
pub(super) const SPAN_LIST: &str = "spans";
pub(super) const TARGET: &str = "target";
pub(super) const THREAD_ID: &str = "threadId";
pub(super) const THREAD_NAME: &str = "threadName";
pub(super) const TIMESTAMP: &str = "timestamp";
Loading

0 comments on commit 22036d8

Please sign in to comment.