Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subscriber: improve docs on Layer composition #566

Merged
merged 3 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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-subscriber/src/fmt/fmt_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use tracing_core::{
/// use tracing_subscriber::{fmt, registry::Registry};
/// use tracing_subscriber::prelude::*;
///
/// let subscriber = fmt::Layer::default()
/// .with_subscriber(Registry::default());
/// let subscriber = Registry::default()
/// .with(fmt::Layer::default());
///
/// tracing::subscriber::set_global_default(subscriber).unwrap();
/// ```
Expand Down
11 changes: 6 additions & 5 deletions tracing-subscriber/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,13 @@
//! let fmt_layer = fmt::Layer::builder()
//! .with_target(false)
//! .finish();
//!
//! let subscriber = EnvFilter::try_from_default_env()
//! let filter_layer = EnvFilter::try_from_default_env()
//! .or_else(|_| EnvFilter::try_new("info"))
//! .unwrap()
//! .and_then(fmt_layer)
//! .with_subscriber(Registry::default());
//! .unwrap();
//!
//! let subscriber = Registry::default()
//! .with(filter_layer)
//! .with(fmt_layer);
//!
//! tracing::subscriber::set_global_default(subscriber).unwrap();
//! ```
Expand Down
124 changes: 119 additions & 5 deletions tracing-subscriber/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,124 @@ use std::{any::TypeId, marker::PhantomData};
/// [`Subscriber`] behavior; it can _observe_ events and spans, but does not
/// assign IDs.
///
/// ## Composing Layers
///
/// Since a `Layer` does not implement a complete strategy for collecting
/// traces, it must be composed with a `Subscriber` in order to be used. The
/// `Layer` trait is generic over a type parameter (called `S` in the trait
/// definition), representing the types of `Subscriber` they can be composed
/// with. Thus, a `Layer` may be implemented that will only compose with a
/// particular `Subscriber` implementation, or additional trait bounds may be
/// added to constrain what types implementing `Subscriber` a `Layer` can wrap.
///
/// `Layer`s may be added to a `Subscriber` by using the [`SubscriberExt::with`]
/// method, which is provided by `tracing-subscriber`'s [prelude]. This method
/// returns a [`Layered`] struct that implements `Subscriber` by composing the
/// `Layer` with the `Subscriber`.
///
/// For example:
/// ```rust
/// use tracing_subscriber::Layer;
/// use tracing_subscriber::prelude::*;
/// use tracing::Subscriber;
///
/// pub struct MyLayer {
/// // ...
/// }
///
/// impl<S: Subscriber> Layer<S> for MyLayer {
/// // ...
/// }
///
/// pub struct MySubscriber {
/// // ...
/// }
///
/// # use tracing_core::{span::{Id, Attributes, Record}, Metadata, Event};
/// impl Subscriber for MySubscriber {
/// // ...
/// # fn new_span(&self, _: &Attributes) -> Id { Id::from_u64(1) }
/// # fn record(&self, _: &Id, _: &Record) {}
/// # fn event(&self, _: &Event) {}
/// # fn record_follows_from(&self, _: &Id, _: &Id) {}
/// # fn enabled(&self, _: &Metadata) -> bool { false }
/// # fn enter(&self, _: &Id) {}
/// # fn exit(&self, _: &Id) {}
/// }
/// # impl MyLayer {
/// # fn new() -> Self { Self {} }
/// # }
/// # impl MySubscriber {
/// # fn new() -> Self { Self { }}
/// # }
///
/// let subscriber = MySubscriber::new()
/// .with(MyLayer::new());
///
/// tracing::subscriber::set_global_default(subscriber);
/// ```
///
/// Multiple `Layer`s may be composed in the same manner:
/// ```rust
/// # use tracing_subscriber::Layer;
/// # use tracing_subscriber::prelude::*;
/// # use tracing::Subscriber;
/// pub struct MyOtherLayer {
/// // ...
/// }
///
/// impl<S: Subscriber> Layer<S> for MyOtherLayer {
/// // ...
/// }
///
/// pub struct MyThirdLayer {
/// // ...
/// }
///
/// impl<S: Subscriber> Layer<S> for MyThirdLayer {
/// // ...
/// }
///
/// # pub struct MySubscriber { }
/// # use tracing_core::{span::{Id, Attributes, Record}, Metadata, Event};
/// # impl Subscriber for MySubscriber {
/// # fn new_span(&self, _: &Attributes) -> Id { Id::from_u64(1) }
/// # fn record(&self, _: &Id, _: &Record) {}
/// # fn event(&self, _: &Event) {}
/// # fn record_follows_from(&self, _: &Id, _: &Id) {}
/// # fn enabled(&self, _: &Metadata) -> bool { false }
/// # fn enter(&self, _: &Id) {}
/// # fn exit(&self, _: &Id) {}
/// }
/// # impl MyLayer {
/// # fn new() -> Self { Self {} }
/// # }
/// # impl MySubscriber {
/// # fn new() -> Self { Self { }}
/// # }
///
/// let subscriber = MySubscriber::new()
/// .with(MyLayer::new())
/// .with(MyOtherLayer::new())
/// .with(MyThirdLayer::new());
///
/// tracing::subscriber::set_global_default(subscriber);
/// ```
///
/// The [`Layer::with_subscriber` method][with-sub] constructs the `Layered`
/// type from a `Layer` and `Subscriber`, and is called by
/// [`SubscriberExt::with`]. In general, it is more idiomatic to use
/// `SubscriberExt::with`, and treat `Layer::with_subscriber` as an
/// implementation detail, as `with_subscriber` calls must be nested, leading to
/// less clear code for the reader. However, `Layer`s which wish to perform
/// additional behavior when composed with a subscriber may provide their own
/// implementations of `SubscriberExt::with`.
///
/// [`SubscriberExt::with`]: trait.SubscriberExt.html#method.with
/// [`Layered`]: struct.Layered.html
/// [prelude]: ../prelude/index.html
/// [with-sub]: #method.with_subscriber
///
/// ## Recording Traces
///
/// The `Layer` trait defines a set of methods for consuming notifications from
Expand Down Expand Up @@ -371,11 +489,7 @@ pub trait SubscriberExt: Subscriber + crate::sealed::Sealed {
L: Layer<Self>,
Self: Sized,
{
Layered {
layer,
inner: self,
_s: PhantomData,
}
layer.with_subscriber(self)
}
}

Expand Down
6 changes: 3 additions & 3 deletions tracing-subscriber/src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
//! # fn new() -> Self { Self { }}
//! # }
//!
//! let subscriber = FooLayer::new()
//! .and_then(BarLayer::new())
//! .with_subscriber(Registry::default());
//! let subscriber = Registry::default()
//! .with(FooLayer::new())
//! .with(BarLayer::new())
//! ```
//!
//! If a type implementing `Layer` depends on the functionality of a `Registry`
Expand Down