Skip to content

Commit 1a095ff

Browse files
committed
feat(subscriber): Add with_span_list implementation for all standard formats.
1 parent c297a37 commit 1a095ff

File tree

6 files changed

+122
-83
lines changed

6 files changed

+122
-83
lines changed

tracing-core/src/field.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,7 @@ mod test {
12381238

12391239
struct MyVisitor;
12401240
impl Visit for MyVisitor {
1241-
fn record_debug(&mut self, field: &Field, _: &dyn (fmt::Debug)) {
1241+
fn record_debug(&mut self, field: &Field, _: &dyn fmt::Debug) {
12421242
assert_eq!(field.callsite(), TEST_META_1.callsite())
12431243
}
12441244
}
@@ -1257,7 +1257,7 @@ mod test {
12571257

12581258
struct MyVisitor;
12591259
impl Visit for MyVisitor {
1260-
fn record_debug(&mut self, field: &Field, _: &dyn (fmt::Debug)) {
1260+
fn record_debug(&mut self, field: &Field, _: &dyn fmt::Debug) {
12611261
assert_eq!(field.name(), "bar")
12621262
}
12631263
}

tracing-subscriber/src/fmt/fmt_layer.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,15 @@ where
484484
}
485485
}
486486

487+
/// Sets whether or not the formatter will include a list (from root to leaf)
488+
/// of all currently entered spans in formatted events.
489+
pub fn with_span_list(self, display_span_list: bool) -> Self {
490+
Layer {
491+
fmt_event: self.fmt_event.with_span_list(display_span_list),
492+
..self
493+
}
494+
}
495+
487496
/// Sets whether or not an event's target is displayed.
488497
pub fn with_target(self, display_target: bool) -> Layer<S, N, format::Format<L, T>, W> {
489498
Layer {
@@ -643,21 +652,6 @@ impl<S, T, W> Layer<S, format::JsonFields, format::Format<format::Json, T>, W> {
643652
..self
644653
}
645654
}
646-
647-
/// Sets whether or not the formatter will include a list (from root to leaf)
648-
/// of all currently entered spans in formatted events.
649-
///
650-
/// See [`format::Json`][super::format::Json]
651-
pub fn with_span_list(
652-
self,
653-
display_span_list: bool,
654-
) -> Layer<S, format::JsonFields, format::Format<format::Json, T>, W> {
655-
Layer {
656-
fmt_event: self.fmt_event.with_span_list(display_span_list),
657-
fmt_fields: format::JsonFields::new(),
658-
..self
659-
}
660-
}
661655
}
662656

663657
impl<S, N, E, W> Layer<S, N, E, W> {

tracing-subscriber/src/fmt/format/json.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ use tracing_log::NormalizeEvent;
6565
/// the root
6666
/// - [`Json::with_current_span`] can be used to control logging of the current
6767
/// span
68-
/// - [`Json::with_span_list`] can be used to control logging of the span list
69-
/// object.
7068
///
7169
/// By default, event fields are not flattened, and both current span and span
7270
/// list are logged.
@@ -84,15 +82,13 @@ use tracing_log::NormalizeEvent;
8482
///
8583
/// [`Json::flatten_event`]: Json::flatten_event()
8684
/// [`Json::with_current_span`]: Json::with_current_span()
87-
/// [`Json::with_span_list`]: Json::with_span_list()
8885
/// [`valuable`]: https://crates.io/crates/valuable
8986
/// [unstable]: crate#unstable-features
9087
/// [`valuable::Valuable`]: https://docs.rs/valuable/latest/valuable/trait.Valuable.html
9188
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
9289
pub struct Json {
9390
pub(crate) flatten_event: bool,
9491
pub(crate) display_current_span: bool,
95-
pub(crate) display_span_list: bool,
9692
}
9793

9894
impl Json {
@@ -105,12 +101,6 @@ impl Json {
105101
pub fn with_current_span(&mut self, display_current_span: bool) {
106102
self.display_current_span = display_current_span;
107103
}
108-
109-
/// If set to `false`, formatted events won't contain a list of all currently
110-
/// entered spans. Spans are logged in a list from root to leaf.
111-
pub fn with_span_list(&mut self, display_span_list: bool) {
112-
self.display_span_list = display_span_list;
113-
}
114104
}
115105

116106
struct SerializableContext<'a, 'b, Span, N>(
@@ -252,8 +242,7 @@ where
252242

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

255-
let current_span = if self.format.display_current_span || self.format.display_span_list
256-
{
245+
let current_span = if self.format.display_current_span || self.display_span_list {
257246
event
258247
.parent()
259248
.and_then(|id| ctx.span(id))
@@ -296,7 +285,7 @@ where
296285
}
297286
}
298287

299-
if self.format.display_span_list && current_span.is_some() {
288+
if self.display_span_list && current_span.is_some() {
300289
serializer.serialize_entry(
301290
"spans",
302291
&SerializableContext(&ctx.ctx, format_field_marker),
@@ -336,7 +325,6 @@ impl Default for Json {
336325
Json {
337326
flatten_event: false,
338327
display_current_span: true,
339-
display_span_list: true,
340328
}
341329
}
342330
}

tracing-subscriber/src/fmt/format/mod.rs

Lines changed: 96 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ pub struct Format<F = Full, T = SystemTime> {
412412
pub(crate) display_thread_name: bool,
413413
pub(crate) display_filename: bool,
414414
pub(crate) display_line_number: bool,
415+
pub(crate) display_span_list: bool,
415416
}
416417

417418
// === impl Writer ===
@@ -604,6 +605,7 @@ impl Default for Format<Full, SystemTime> {
604605
display_thread_name: false,
605606
display_filename: false,
606607
display_line_number: false,
608+
display_span_list: true,
607609
}
608610
}
609611
}
@@ -624,6 +626,7 @@ impl<F, T> Format<F, T> {
624626
display_thread_name: self.display_thread_name,
625627
display_filename: self.display_filename,
626628
display_line_number: self.display_line_number,
629+
display_span_list: self.display_span_list,
627630
}
628631
}
629632

@@ -663,6 +666,7 @@ impl<F, T> Format<F, T> {
663666
display_thread_name: self.display_thread_name,
664667
display_filename: true,
665668
display_line_number: true,
669+
display_span_list: self.display_span_list,
666670
}
667671
}
668672

@@ -694,6 +698,7 @@ impl<F, T> Format<F, T> {
694698
display_thread_name: self.display_thread_name,
695699
display_filename: self.display_filename,
696700
display_line_number: self.display_line_number,
701+
display_span_list: self.display_span_list,
697702
}
698703
}
699704

@@ -723,6 +728,7 @@ impl<F, T> Format<F, T> {
723728
display_thread_name: self.display_thread_name,
724729
display_filename: self.display_filename,
725730
display_line_number: self.display_line_number,
731+
display_span_list: self.display_span_list,
726732
}
727733
}
728734

@@ -739,6 +745,7 @@ impl<F, T> Format<F, T> {
739745
display_thread_name: self.display_thread_name,
740746
display_filename: self.display_filename,
741747
display_line_number: self.display_line_number,
748+
display_span_list: self.display_span_list,
742749
}
743750
}
744751

@@ -820,6 +827,15 @@ impl<F, T> Format<F, T> {
820827
.with_file(display_location)
821828
}
822829

830+
/// If set to `false`, formatted events won't contain a list of all currently
831+
/// entered spans. Spans are logged in a list from root to leaf.
832+
pub fn with_span_list(self, display_span_list: bool) -> Self {
833+
Format {
834+
display_span_list,
835+
..self
836+
}
837+
}
838+
823839
#[inline]
824840
fn format_timestamp(&self, writer: &mut Writer<'_>) -> fmt::Result
825841
where
@@ -887,17 +903,6 @@ impl<T> Format<Json, T> {
887903
self.format.with_current_span(display_current_span);
888904
self
889905
}
890-
891-
/// Sets whether or not the formatter will include a list (from root to
892-
/// leaf) of all currently entered spans in formatted events.
893-
///
894-
/// See [`format::Json`][Json]
895-
#[cfg(feature = "json")]
896-
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
897-
pub fn with_span_list(mut self, display_span_list: bool) -> Format<Json, T> {
898-
self.format.with_span_list(display_span_list);
899-
self
900-
}
901906
}
902907

903908
impl<S, N, T> FormatEvent<S, N> for Format<Full, T>
@@ -963,25 +968,34 @@ where
963968

964969
let dimmed = writer.dimmed();
965970

966-
if let Some(scope) = ctx.event_scope() {
971+
if let Some(mut scope) = ctx.event_scope() {
967972
let bold = writer.bold();
968-
969-
let mut seen = false;
970-
971-
for span in scope.from_root() {
973+
if self.display_span_list {
974+
let scope_from_root = scope.from_root();
975+
let mut seen = false;
976+
for span in scope_from_root {
977+
write!(writer, "{}", bold.paint(span.metadata().name()))?;
978+
seen = true;
979+
let ext = span.extensions();
980+
if let Some(fields) = &ext.get::<FormattedFields<N>>() {
981+
if !fields.is_empty() {
982+
write!(writer, "{}{}{}", bold.paint("{"), fields, bold.paint("}"))?;
983+
}
984+
}
985+
write!(writer, "{}", dimmed.paint(":"))?;
986+
}
987+
if seen {
988+
writer.write_char(' ')?;
989+
}
990+
} else if let Some(span) = scope.next() {
972991
write!(writer, "{}", bold.paint(span.metadata().name()))?;
973-
seen = true;
974-
975992
let ext = span.extensions();
976993
if let Some(fields) = &ext.get::<FormattedFields<N>>() {
977994
if !fields.is_empty() {
978995
write!(writer, "{}{}{}", bold.paint("{"), fields, bold.paint("}"))?;
979996
}
980997
}
981998
write!(writer, "{}", dimmed.paint(":"))?;
982-
}
983-
984-
if seen {
985999
writer.write_char(' ')?;
9861000
}
9871001
};
@@ -1092,11 +1106,16 @@ where
10921106
let fmt_ctx = {
10931107
#[cfg(feature = "ansi")]
10941108
{
1095-
FmtCtx::new(ctx, event.parent(), writer.has_ansi_escapes())
1109+
FmtCtx::new(
1110+
ctx,
1111+
event.parent(),
1112+
self.display_span_list,
1113+
writer.has_ansi_escapes(),
1114+
)
10961115
}
10971116
#[cfg(not(feature = "ansi"))]
10981117
{
1099-
FmtCtx::new(&ctx, event.parent())
1118+
FmtCtx::new(&ctx, event.parent(), self.display_span_list)
11001119
}
11011120
};
11021121
write!(writer, "{}", fmt_ctx)?;
@@ -1144,11 +1163,20 @@ where
11441163

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

1147-
for span in ctx
1148-
.event_scope()
1149-
.into_iter()
1150-
.flat_map(crate::registry::Scope::from_root)
1151-
{
1166+
if self.display_span_list {
1167+
for span in ctx
1168+
.event_scope()
1169+
.into_iter()
1170+
.flat_map(crate::registry::Scope::from_root)
1171+
{
1172+
let exts = span.extensions();
1173+
if let Some(fields) = exts.get::<FormattedFields<N>>() {
1174+
if !fields.is_empty() {
1175+
write!(writer, " {}", dimmed.paint(&fields.fields))?;
1176+
}
1177+
}
1178+
}
1179+
} else if let Some(span) = ctx.event_scope().as_mut().and_then(|scope| scope.next()) {
11521180
let exts = span.extensions();
11531181
if let Some(fields) = exts.get::<FormattedFields<N>>() {
11541182
if !fields.is_empty() {
@@ -1347,6 +1375,7 @@ impl Display for ErrorSourceList<'_> {
13471375
struct FmtCtx<'a, S, N> {
13481376
ctx: &'a FmtContext<'a, S, N>,
13491377
span: Option<&'a span::Id>,
1378+
display_span_list: bool,
13501379
#[cfg(feature = "ansi")]
13511380
ansi: bool,
13521381
}
@@ -1360,14 +1389,28 @@ where
13601389
pub(crate) fn new(
13611390
ctx: &'a FmtContext<'_, S, N>,
13621391
span: Option<&'a span::Id>,
1392+
display_span_list: bool,
13631393
ansi: bool,
13641394
) -> Self {
1365-
Self { ctx, span, ansi }
1395+
Self {
1396+
ctx,
1397+
span,
1398+
ansi,
1399+
display_span_list,
1400+
}
13661401
}
13671402

13681403
#[cfg(not(feature = "ansi"))]
1369-
pub(crate) fn new(ctx: &'a FmtContext<'_, S, N>, span: Option<&'a span::Id>) -> Self {
1370-
Self { ctx, span }
1404+
pub(crate) fn new(
1405+
ctx: &'a FmtContext<'_, S, N>,
1406+
span: Option<&'a span::Id>,
1407+
display_span_list: bool,
1408+
) -> Self {
1409+
Self {
1410+
ctx,
1411+
span,
1412+
display_span_list,
1413+
}
13711414
}
13721415

13731416
fn bold(&self) -> Style {
@@ -1396,14 +1439,19 @@ where
13961439
.and_then(|id| self.ctx.ctx.span(id))
13971440
.or_else(|| self.ctx.ctx.lookup_current());
13981441

1399-
let scope = span.into_iter().flat_map(|span| span.scope().from_root());
1442+
if self.display_span_list {
1443+
let scope = span.into_iter().flat_map(|span| span.scope().from_root());
14001444

1401-
for span in scope {
1402-
seen = true;
1403-
write!(f, "{}:", bold.paint(span.metadata().name()))?;
1404-
}
1445+
for span in scope {
1446+
seen = true;
1447+
write!(f, "{}:", bold.paint(span.metadata().name()))?;
1448+
}
14051449

1406-
if seen {
1450+
if seen {
1451+
f.write_char(' ')?;
1452+
}
1453+
} else if let Some(span) = span.iter().next() {
1454+
write!(f, "{}:", bold.paint(span.metadata().name()))?;
14071455
f.write_char(' ')?;
14081456
}
14091457
Ok(())
@@ -2111,6 +2159,17 @@ pub(super) mod test {
21112159
test_overridden_parents(expected, crate::fmt::Subscriber::builder().compact())
21122160
}
21132161

2162+
#[test]
2163+
fn overridden_parents_with_span_list_false() {
2164+
let expected = "fake time span2: tracing_subscriber::fmt::format::test: hello span=2\n";
2165+
test_overridden_parents(
2166+
expected,
2167+
crate::fmt::Subscriber::builder()
2168+
.compact()
2169+
.with_span_list(false),
2170+
)
2171+
}
2172+
21142173
#[test]
21152174
fn overridden_parents_in_scope() {
21162175
test_overridden_parents_in_scope(

0 commit comments

Comments
 (0)