Skip to content

Commit 54eff34

Browse files
davidpdrsnhawkw
andcommitted
subscriber: add sample implementation of FormatEvent (#1189)
* Add sample implementation of `FormatEvent` The docs previously didn't provide much help implementing `FormatEvent`. Especially getting access to fields on spans could be tricky unless the user was aware of `FormattedFields`. This updates the docs with a basic, but working, example. * Apply suggestions from code review Co-authored-by: Eliza Weisman <[email protected]> * Add comment explaining what `FormattedFields` is * Expand docs a bit * Fix formatting Co-authored-by: Eliza Weisman <[email protected]>
1 parent cd79b11 commit 54eff34

File tree

1 file changed

+68
-0
lines changed
  • tracing-subscriber/src/fmt/format

1 file changed

+68
-0
lines changed

Diff for: tracing-subscriber/src/fmt/format/mod.rs

+68
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,74 @@ use fmt::{Debug, Display};
4545
/// This trait is already implemented for function pointers with the same
4646
/// signature as `format_event`.
4747
///
48+
/// # Examples
49+
///
50+
/// ```rust
51+
/// use std::fmt::{self, Write};
52+
/// use tracing_core::{Subscriber, Event};
53+
/// use tracing_subscriber::fmt::{FormatEvent, FormatFields, FmtContext, FormattedFields};
54+
/// use tracing_subscriber::registry::LookupSpan;
55+
///
56+
/// struct MyFormatter;
57+
///
58+
/// impl<S, N> FormatEvent<S, N> for MyFormatter
59+
/// where
60+
/// S: Subscriber + for<'a> LookupSpan<'a>,
61+
/// N: for<'a> FormatFields<'a> + 'static,
62+
/// {
63+
/// fn format_event(
64+
/// &self,
65+
/// ctx: &FmtContext<'_, S, N>,
66+
/// writer: &mut dyn fmt::Write,
67+
/// event: &Event<'_>,
68+
/// ) -> fmt::Result {
69+
/// // Write level and target
70+
/// let level = *event.metadata().level();
71+
/// let target = event.metadata().target();
72+
/// write!(
73+
/// writer,
74+
/// "{} {}: ",
75+
/// level,
76+
/// target,
77+
/// )?;
78+
///
79+
/// // Write spans and fields of each span
80+
/// ctx.visit_spans(|span| {
81+
/// write!(writer, "{}", span.name())?;
82+
///
83+
/// let ext = span.extensions();
84+
///
85+
/// // `FormattedFields` is a a formatted representation of the span's
86+
/// // fields, which is stored in its extensions by the `fmt` layer's
87+
/// // `new_span` method. The fields will have been formatted
88+
/// // by the same field formatter that's provided to the event
89+
/// // formatter in the `FmtContext`.
90+
/// let fields = &ext
91+
/// .get::<FormattedFields<N>>()
92+
/// .expect("will never be `None`");
93+
///
94+
/// if !fields.is_empty() {
95+
/// write!(writer, "{{{}}}", fields)?;
96+
/// }
97+
/// write!(writer, ": ")?;
98+
///
99+
/// Ok(())
100+
/// })?;
101+
///
102+
/// // Write fields on the event
103+
/// ctx.field_format().format_fields(writer, event)?;
104+
///
105+
/// writeln!(writer)
106+
/// }
107+
/// }
108+
/// ```
109+
///
110+
/// This formatter will print events like this:
111+
///
112+
/// ```text
113+
/// DEBUG yak_shaving::shaver: some-span{field-on-span=foo}: started shaving yak
114+
/// ```
115+
///
48116
/// [`fmt::Subscriber`]: ../struct.Subscriber.html
49117
/// [`fmt::Layer`]: ../struct.Layer.html
50118
pub trait FormatEvent<S, N>

0 commit comments

Comments
 (0)