Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tracing-core/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,7 @@ mod test {

struct MyVisitor;
impl Visit for MyVisitor {
fn record_debug(&mut self, field: &Field, _: &dyn (fmt::Debug)) {
fn record_debug(&mut self, field: &Field, _: &dyn fmt::Debug) {
assert_eq!(field.callsite(), TEST_META_1.callsite())
}
}
Expand All @@ -1257,7 +1257,7 @@ mod test {

struct MyVisitor;
impl Visit for MyVisitor {
fn record_debug(&mut self, field: &Field, _: &dyn (fmt::Debug)) {
fn record_debug(&mut self, field: &Field, _: &dyn fmt::Debug) {
assert_eq!(field.name(), "bar")
}
}
Expand Down
24 changes: 9 additions & 15 deletions tracing-subscriber/src/fmt/fmt_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,15 @@ where
}
}

/// Sets whether or not the formatter will include a list (from root to leaf)
/// of all currently entered spans in formatted events.
pub fn with_span_list(self, display_span_list: bool) -> Self {
Layer {
fmt_event: self.fmt_event.with_span_list(display_span_list),
..self
}
}

/// Sets whether or not an event's target is displayed.
pub fn with_target(self, display_target: bool) -> Layer<S, N, format::Format<L, T>, W> {
Layer {
Expand Down Expand Up @@ -643,21 +652,6 @@ impl<S, T, W> Layer<S, format::JsonFields, format::Format<format::Json, T>, W> {
..self
}
}

/// Sets whether or not the formatter will include a list (from root to leaf)
/// of all currently entered spans in formatted events.
///
/// See [`format::Json`][super::format::Json]
pub fn with_span_list(
self,
display_span_list: bool,
) -> Layer<S, format::JsonFields, format::Format<format::Json, T>, W> {
Layer {
fmt_event: self.fmt_event.with_span_list(display_span_list),
fmt_fields: format::JsonFields::new(),
..self
}
}
}

impl<S, N, E, W> Layer<S, N, E, W> {
Expand Down
16 changes: 2 additions & 14 deletions tracing-subscriber/src/fmt/format/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ use tracing_log::NormalizeEvent;
/// the root
/// - [`Json::with_current_span`] can be used to control logging of the current
/// span
/// - [`Json::with_span_list`] can be used to control logging of the span list
/// object.
///
/// By default, event fields are not flattened, and both current span and span
/// list are logged.
Expand All @@ -84,15 +82,13 @@ use tracing_log::NormalizeEvent;
///
/// [`Json::flatten_event`]: Json::flatten_event()
/// [`Json::with_current_span`]: Json::with_current_span()
/// [`Json::with_span_list`]: Json::with_span_list()
/// [`valuable`]: https://crates.io/crates/valuable
/// [unstable]: crate#unstable-features
/// [`valuable::Valuable`]: https://docs.rs/valuable/latest/valuable/trait.Valuable.html
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Json {
pub(crate) flatten_event: bool,
pub(crate) display_current_span: bool,
pub(crate) display_span_list: bool,
}

impl Json {
Expand All @@ -105,12 +101,6 @@ impl Json {
pub fn with_current_span(&mut self, display_current_span: bool) {
self.display_current_span = display_current_span;
}

/// If set to `false`, formatted events won't contain a list of all currently
/// entered spans. Spans are logged in a list from root to leaf.
pub fn with_span_list(&mut self, display_span_list: bool) {
self.display_span_list = display_span_list;
}
}

struct SerializableContext<'a, 'b, Span, N>(
Expand Down Expand Up @@ -252,8 +242,7 @@ where

let format_field_marker: std::marker::PhantomData<N> = std::marker::PhantomData;

let current_span = if self.format.display_current_span || self.format.display_span_list
{
let current_span = if self.format.display_current_span || self.display_span_list {
event
.parent()
.and_then(|id| ctx.span(id))
Expand Down Expand Up @@ -296,7 +285,7 @@ where
}
}

if self.format.display_span_list && current_span.is_some() {
if self.display_span_list && current_span.is_some() {
serializer.serialize_entry(
"spans",
&SerializableContext(&ctx.ctx, format_field_marker),
Expand Down Expand Up @@ -336,7 +325,6 @@ impl Default for Json {
Json {
flatten_event: false,
display_current_span: true,
display_span_list: true,
}
}
}
Expand Down
133 changes: 96 additions & 37 deletions tracing-subscriber/src/fmt/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ pub struct Format<F = Full, T = SystemTime> {
pub(crate) display_thread_name: bool,
pub(crate) display_filename: bool,
pub(crate) display_line_number: bool,
pub(crate) display_span_list: bool,
}

// === impl Writer ===
Expand Down Expand Up @@ -604,6 +605,7 @@ impl Default for Format<Full, SystemTime> {
display_thread_name: false,
display_filename: false,
display_line_number: false,
display_span_list: true,
}
}
}
Expand All @@ -624,6 +626,7 @@ impl<F, T> Format<F, T> {
display_thread_name: self.display_thread_name,
display_filename: self.display_filename,
display_line_number: self.display_line_number,
display_span_list: self.display_span_list,
}
}

Expand Down Expand Up @@ -663,6 +666,7 @@ impl<F, T> Format<F, T> {
display_thread_name: self.display_thread_name,
display_filename: true,
display_line_number: true,
display_span_list: self.display_span_list,
}
}

Expand Down Expand Up @@ -694,6 +698,7 @@ impl<F, T> Format<F, T> {
display_thread_name: self.display_thread_name,
display_filename: self.display_filename,
display_line_number: self.display_line_number,
display_span_list: self.display_span_list,
}
}

Expand Down Expand Up @@ -723,6 +728,7 @@ impl<F, T> Format<F, T> {
display_thread_name: self.display_thread_name,
display_filename: self.display_filename,
display_line_number: self.display_line_number,
display_span_list: self.display_span_list,
}
}

Expand All @@ -739,6 +745,7 @@ impl<F, T> Format<F, T> {
display_thread_name: self.display_thread_name,
display_filename: self.display_filename,
display_line_number: self.display_line_number,
display_span_list: self.display_span_list,
}
}

Expand Down Expand Up @@ -820,6 +827,15 @@ impl<F, T> Format<F, T> {
.with_file(display_location)
}

/// If set to `false`, formatted events won't contain a list of all currently
/// entered spans. Spans are logged in a list from root to leaf.
pub fn with_span_list(self, display_span_list: bool) -> Self {
Format {
display_span_list,
..self
}
}

#[inline]
fn format_timestamp(&self, writer: &mut Writer<'_>) -> fmt::Result
where
Expand Down Expand Up @@ -887,17 +903,6 @@ impl<T> Format<Json, T> {
self.format.with_current_span(display_current_span);
self
}

/// Sets whether or not the formatter will include a list (from root to
/// leaf) of all currently entered spans in formatted events.
///
/// See [`format::Json`][Json]
#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
pub fn with_span_list(mut self, display_span_list: bool) -> Format<Json, T> {
self.format.with_span_list(display_span_list);
self
}
}

impl<S, N, T> FormatEvent<S, N> for Format<Full, T>
Expand Down Expand Up @@ -963,25 +968,34 @@ where

let dimmed = writer.dimmed();

if let Some(scope) = ctx.event_scope() {
if let Some(mut scope) = ctx.event_scope() {
let bold = writer.bold();

let mut seen = false;

for span in scope.from_root() {
if self.display_span_list {
let scope_from_root = scope.from_root();
let mut seen = false;
for span in scope_from_root {
write!(writer, "{}", bold.paint(span.metadata().name()))?;
seen = true;
let ext = span.extensions();
if let Some(fields) = &ext.get::<FormattedFields<N>>() {
if !fields.is_empty() {
write!(writer, "{}{}{}", bold.paint("{"), fields, bold.paint("}"))?;
}
}
write!(writer, "{}", dimmed.paint(":"))?;
}
if seen {
writer.write_char(' ')?;
}
} else if let Some(span) = scope.next() {
write!(writer, "{}", bold.paint(span.metadata().name()))?;
seen = true;

let ext = span.extensions();
if let Some(fields) = &ext.get::<FormattedFields<N>>() {
if !fields.is_empty() {
write!(writer, "{}{}{}", bold.paint("{"), fields, bold.paint("}"))?;
}
}
write!(writer, "{}", dimmed.paint(":"))?;
}

if seen {
writer.write_char(' ')?;
}
};
Expand Down Expand Up @@ -1092,11 +1106,16 @@ where
let fmt_ctx = {
#[cfg(feature = "ansi")]
{
FmtCtx::new(ctx, event.parent(), writer.has_ansi_escapes())
FmtCtx::new(
ctx,
event.parent(),
self.display_span_list,
writer.has_ansi_escapes(),
)
}
#[cfg(not(feature = "ansi"))]
{
FmtCtx::new(&ctx, event.parent())
FmtCtx::new(&ctx, event.parent(), self.display_span_list)
}
};
write!(writer, "{}", fmt_ctx)?;
Expand Down Expand Up @@ -1144,11 +1163,20 @@ where

ctx.format_fields(writer.by_ref(), event)?;

for span in ctx
.event_scope()
.into_iter()
.flat_map(crate::registry::Scope::from_root)
{
if self.display_span_list {
for span in ctx
.event_scope()
.into_iter()
.flat_map(crate::registry::Scope::from_root)
{
let exts = span.extensions();
if let Some(fields) = exts.get::<FormattedFields<N>>() {
if !fields.is_empty() {
write!(writer, " {}", dimmed.paint(&fields.fields))?;
}
}
}
} else if let Some(span) = ctx.event_scope().as_mut().and_then(|scope| scope.next()) {
let exts = span.extensions();
if let Some(fields) = exts.get::<FormattedFields<N>>() {
if !fields.is_empty() {
Expand Down Expand Up @@ -1347,6 +1375,7 @@ impl Display for ErrorSourceList<'_> {
struct FmtCtx<'a, S, N> {
ctx: &'a FmtContext<'a, S, N>,
span: Option<&'a span::Id>,
display_span_list: bool,
#[cfg(feature = "ansi")]
ansi: bool,
}
Expand All @@ -1360,14 +1389,28 @@ where
pub(crate) fn new(
ctx: &'a FmtContext<'_, S, N>,
span: Option<&'a span::Id>,
display_span_list: bool,
ansi: bool,
) -> Self {
Self { ctx, span, ansi }
Self {
ctx,
span,
ansi,
display_span_list,
}
}

#[cfg(not(feature = "ansi"))]
pub(crate) fn new(ctx: &'a FmtContext<'_, S, N>, span: Option<&'a span::Id>) -> Self {
Self { ctx, span }
pub(crate) fn new(
ctx: &'a FmtContext<'_, S, N>,
span: Option<&'a span::Id>,
display_span_list: bool,
) -> Self {
Self {
ctx,
span,
display_span_list,
}
}

fn bold(&self) -> Style {
Expand Down Expand Up @@ -1396,14 +1439,19 @@ where
.and_then(|id| self.ctx.ctx.span(id))
.or_else(|| self.ctx.ctx.lookup_current());

let scope = span.into_iter().flat_map(|span| span.scope().from_root());
if self.display_span_list {
let scope = span.into_iter().flat_map(|span| span.scope().from_root());

for span in scope {
seen = true;
write!(f, "{}:", bold.paint(span.metadata().name()))?;
}
for span in scope {
seen = true;
write!(f, "{}:", bold.paint(span.metadata().name()))?;
}

if seen {
if seen {
f.write_char(' ')?;
}
} else if let Some(span) = span.iter().next() {
write!(f, "{}:", bold.paint(span.metadata().name()))?;
f.write_char(' ')?;
}
Ok(())
Expand Down Expand Up @@ -2111,6 +2159,17 @@ pub(super) mod test {
test_overridden_parents(expected, crate::fmt::Subscriber::builder().compact())
}

#[test]
fn overridden_parents_with_span_list_false() {
let expected = "fake time span2: tracing_subscriber::fmt::format::test: hello span=2\n";
test_overridden_parents(
expected,
crate::fmt::Subscriber::builder()
.compact()
.with_span_list(false),
)
}

#[test]
fn overridden_parents_in_scope() {
test_overridden_parents_in_scope(
Expand Down
Loading