diff --git a/tracing-serde/src/lib.rs b/tracing-serde/src/lib.rs index 801280e0f..eb29e8180 100644 --- a/tracing-serde/src/lib.rs +++ b/tracing-serde/src/lib.rs @@ -75,6 +75,12 @@ //! next_id: AtomicUsize, // you need to assign span IDs, so you need a counter //! } //! +//! impl JsonSubscriber { +//! fn new() -> Self { +//! Self { next_id: 1.into() } +//! } +//! } +//! //! impl Subscriber for JsonSubscriber { //! //! fn new_span(&self, attrs: &Attributes<'_>) -> Id { @@ -97,7 +103,7 @@ //! } //! //! // ... -//! # fn enabled(&self, _: &Metadata<'_>) -> bool { false } +//! # fn enabled(&self, _: &Metadata<'_>) -> bool { true } //! # fn enter(&self, _: &Id) {} //! # fn exit(&self, _: &Id) {} //! # fn record(&self, _: &Id, _: &Record<'_>) {} @@ -199,6 +205,18 @@ use tracing_core::{ pub mod fields; +#[derive(Debug)] +pub struct SerializeField<'a>(&'a Field); + +impl<'a> Serialize for SerializeField<'a> { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_str(self.0.name()) + } +} + #[derive(Debug)] pub struct SerializeFieldSet<'a>(&'a FieldSet); @@ -209,7 +227,7 @@ impl<'a> Serialize for SerializeFieldSet<'a> { { let mut seq = serializer.serialize_seq(Some(self.0.len()))?; for element in self.0 { - seq.serialize_element(element.name())?; + seq.serialize_element(&SerializeField(&element))?; } seq.end() } @@ -552,6 +570,14 @@ impl<'a> AsSerde<'a> for Level { } } +impl<'a> AsSerde<'a> for Field { + type Serializable = SerializeField<'a>; + + fn as_serde(&'a self) -> Self::Serializable { + SerializeField(self) + } +} + impl<'a> AsSerde<'a> for FieldSet { type Serializable = SerializeFieldSet<'a>; @@ -572,6 +598,8 @@ impl<'a> self::sealed::Sealed for Record<'a> {} impl<'a> self::sealed::Sealed for Metadata<'a> {} +impl self::sealed::Sealed for Field {} + impl self::sealed::Sealed for FieldSet {} mod sealed {