pub struct TracingLayer { /* private fields */ }Available on crate feature
tracing and (crate features log or tracing) only.Expand description
A tracing layer that writes events through a Logger.
- Event levels map one to one; the event’s target, file and line are preserved.
- The
messagefield becomes the record message; every other event field becomes a record field. Integers, floats and booleans keep their type; strings,Debugvalues and errors are rendered to text. - Fields of the spans enclosing the event (root first) are attached as context. A field on an inner span shadows the same field on an outer span, and event fields shadow span fields.
- The logger’s filter is applied per event. The layer does not
implement
Layer::enabled, because returningfalsethere would disable the event for every other layer too; combine it withtracing-subscriber’s own filters if you need callsite-level filtering.
Event fields are collected into a reused thread-local buffer, so an event does not allocate in steady state; each span’s fields are stored once, when the span is created or recorded.
The layer needs a subscriber that stores spans, such as
tracing_subscriber::Registry (enable tracing-subscriber’s
registry feature in your own Cargo.toml).
§Examples
use log_io::compat::TracingLayer;
use log_io::{Level, Logger};
use tracing_subscriber::layer::SubscriberExt;
let logger = Logger::builder().level(Level::Info).stdout_json().build();
let subscriber = tracing_subscriber::registry().with(TracingLayer::new(logger));
tracing::subscriber::with_default(subscriber, || {
let span = tracing::info_span!("request", id = 7);
let _entered = span.enter();
tracing::info!(status = 200, "handled");
});Implementations§
Trait Implementations§
Source§impl Clone for TracingLayer
impl Clone for TracingLayer
Source§impl Debug for TracingLayer
impl Debug for TracingLayer
Source§impl<S> Layer<S> for TracingLayerwhere
S: Subscriber + for<'lookup> LookupSpan<'lookup>,
impl<S> Layer<S> for TracingLayerwhere
S: Subscriber + for<'lookup> LookupSpan<'lookup>,
Source§fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>)
fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>)
Notifies this layer that a new span was constructed with the given
Attributes and Id.Source§fn on_record(&self, id: &Id, values: &SpanRecord<'_>, ctx: Context<'_, S>)
fn on_record(&self, id: &Id, values: &SpanRecord<'_>, ctx: Context<'_, S>)
Notifies this layer that a span with the given
Id recorded the given
values.Source§fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>)
fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>)
Notifies this layer that an event has occurred.
Source§fn on_register_dispatch(&self, subscriber: &Dispatch)
fn on_register_dispatch(&self, subscriber: &Dispatch)
Performs late initialization when installing this layer as a
Subscriber. Read moreSource§fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest
fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest
Registers a new callsite with this layer, returning whether or not
the layer is interested in being notified about the callsite, similarly
to
Subscriber::register_callsite. Read moreSource§fn enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool
fn enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool
Returns
true if this layer is interested in a span or event with the
given metadata in the current Context, similarly to
Subscriber::enabled. Read moreSource§fn on_follows_from(&self, _span: &Id, _follows: &Id, _ctx: Context<'_, S>)
fn on_follows_from(&self, _span: &Id, _follows: &Id, _ctx: Context<'_, S>)
Notifies this layer that a span with the ID
span recorded that it
follows from the span with the ID follows.Source§fn on_enter(&self, _id: &Id, _ctx: Context<'_, S>)
fn on_enter(&self, _id: &Id, _ctx: Context<'_, S>)
Notifies this layer that a span with the given ID was entered.
Source§fn on_exit(&self, _id: &Id, _ctx: Context<'_, S>)
fn on_exit(&self, _id: &Id, _ctx: Context<'_, S>)
Notifies this layer that the span with the given ID was exited.
Source§fn on_close(&self, _id: Id, _ctx: Context<'_, S>)
fn on_close(&self, _id: Id, _ctx: Context<'_, S>)
Notifies this layer that the span with the given ID has been closed.
Source§fn on_id_change(&self, _old: &Id, _new: &Id, _ctx: Context<'_, S>)
fn on_id_change(&self, _old: &Id, _new: &Id, _ctx: Context<'_, S>)
Notifies this layer that a span ID has been cloned, and that the
subscriber returned a different ID.
Source§fn and_then<L>(self, layer: L) -> Layered<L, Self, S>
fn and_then<L>(self, layer: L) -> Layered<L, Self, S>
Composes this layer around the given
Layer, returning a Layered
struct implementing Layer. Read moreSource§fn with_subscriber(self, inner: S) -> Layered<Self, S>where
Self: Sized,
fn with_subscriber(self, inner: S) -> Layered<Self, S>where
Self: Sized,
Composes this
Layer with the given Subscriber, returning a
Layered struct that implements Subscriber. Read moreAuto Trait Implementations§
impl Forget for TracingLayer
impl Freeze for TracingLayer
impl Move for TracingLayer
impl RefUnwindSafe for TracingLayer
impl Send for TracingLayer
impl Sync for TracingLayer
impl Unpin for TracingLayer
impl UnsafeUnpin for TracingLayer
impl UnwindSafe for TracingLayer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more