tracing_subscriber/lib.rs
1//! Utilities for implementing and composing [`tracing`] subscribers.
2//!
3//! [`tracing`] is a framework for instrumenting Rust programs to collect
4//! scoped, structured, and async-aware diagnostics. The [`Collect`] trait
5//! represents the functionality necessary to collect this trace data. This
6//! crate contains tools for composing subscribers out of smaller units of
7//! behaviour, and batteries-included implementations of common subscriber
8//! functionality.
9//!
10//! `tracing-subscriber` is intended for use by both `Collector` authors and
11//! application authors using `tracing` to instrument their applications.
12//!
13//! *Compiler support: [requires `rustc` 1.63+][msrv]*
14//!
15//! [msrv]: #supported-rust-versions
16//!
17//! ## Subscribers and Filters
18//!
19//! The most important component of the `tracing-subscriber` API is the
20//! [`Subscribe`] trait, which provides a composable abstraction for building
21//! [collector]s. Like the [`Collect`] trait, [`Subscribe`] defines a
22//! particular behavior for collecting trace data. Unlike [`Collect`],
23//! which implements a *complete* strategy for how trace data is collected,
24//! [`Subscribe`] provide *modular* implementations of specific behaviors.
25//! Therefore, they can be [composed together] to form a [collector] which is
26//! capable of recording traces in a variety of ways. See the [`subscribe` module's
27//! documentation][subscribe] for details on using [subscribers].
28//!
29//! In addition, the [`Filter`] trait defines an interface for filtering what
30//! spans and events are recorded by a particular subscriber. This allows different
31//! [`Subscribe`] implementationss to handle separate subsets of the trace data
32//! emitted by a program. See the [documentation on per-subscriber
33//! filtering][psf] for more information on using [`Filter`]s.
34//!
35//! [`Subscribe`]: crate::subscribe::Subscribe
36//! [composed together]: crate::subscribe#composing-subscribers
37//! [subscribe]: crate::subscribe
38//! [subscribers]: crate::subscribe
39//! [`Filter`]: crate::subscribe::Filter
40//! [psf]: crate::subscribe#per-subscriber-filtering
41//!
42//! ## Included Collectors
43//!
44//! The following [collector]s are provided for application authors:
45//!
46//! - [`fmt`] - Formats and logs tracing data (requires the `fmt` feature flag)
47//!
48//! ## Feature Flags
49//!
50//! - `std`: Enables APIs that depend on the Rust standard library
51//! (enabled by default).
52//! - `alloc`: Depend on [`liballoc`] (enabled by "std").
53//! - `env-filter`: Enables the [`EnvFilter`] type, which implements filtering
54//! similar to the [`env_logger` crate]. **Requires "std"**.
55//! - `fmt`: Enables the [`fmt`] module, which provides a subscriber
56//! implementation for printing formatted representations of trace events.
57//! Enabled by default. **Requires "registry" and "std"**.
58//! - `ansi`: Enables `fmt` support for ANSI terminal colors. Enabled by
59//! default.
60//! - `registry`: enables the [`registry`] module. Enabled by default.
61//! **Requires "std"**.
62//! - `json`: Enables `fmt` support for JSON output. In JSON output, the ANSI
63//! feature does nothing. **Requires "fmt" and "std"**.
64//! - `local-time`: Enables local time formatting when using the [`time`
65//! crate]'s timestamp formatters with the `fmt` subscriber.
66//!
67//! ### Optional Dependencies
68//!
69//! - [`tracing-log`]: Enables better formatting for events emitted by `log`
70//! macros in the `fmt` subscriber. Enabled by default.
71//! - [`time`][`time` crate]: Enables support for using the [`time` crate] for timestamp
72//! formatting in the `fmt` subscriber.
73//! - [`smallvec`]: Causes the `EnvFilter` type to use the `smallvec` crate (rather
74//! than `Vec`) as a performance optimization. Enabled by default.
75//! - [`parking_lot`]: Use the `parking_lot` crate's `RwLock` implementation
76//! rather than the Rust standard library's implementation.
77//!
78//! ### `no_std` Support
79//!
80//! In embedded systems and other bare-metal applications, `tracing` can be
81//! used without requiring the Rust standard library, although some features are
82//! disabled. Although most of the APIs provided by `tracing-subscriber`, such
83//! as [`fmt`] and [`EnvFilter`], require the standard library, some
84//! functionality, such as the [`Subscribe`] trait, can still be used in
85//! `no_std` environments.
86//!
87//! The dependency on the standard library is controlled by two crate feature
88//! flags, "std", which enables the dependency on [`libstd`], and "alloc", which
89//! enables the dependency on [`liballoc`] (and is enabled by the "std"
90//! feature). These features are enabled by default, but `no_std` users can
91//! disable them using:
92//!
93//! ```toml
94//! # Cargo.toml
95//! tracing-subscriber = { version = "0.3", default-features = false }
96//! ```
97//!
98//! Additional APIs are available when [`liballoc`] is available. To enable
99//! `liballoc` but not `std`, use:
100//!
101//! ```toml
102//! # Cargo.toml
103//! tracing-subscriber = { version = "0.3", default-features = false, features = ["alloc"] }
104//! ```
105//!
106//! ## Supported Rust Versions
107//!
108//! Tracing is built against the latest stable release. The minimum supported
109//! version is 1.63. The current Tracing version is not guaranteed to build on
110//! Rust versions earlier than the minimum supported version.
111//!
112//! Tracing follows the same compiler support policies as the rest of the Tokio
113//! project. The current stable Rust compiler and the three most recent minor
114//! versions before it will always be supported. For example, if the current
115//! stable compiler version is 1.69, the minimum supported version will not be
116//! increased past 1.66, three minor versions prior. Increasing the minimum
117//! supported compiler version is not considered a semver breaking change as
118//! long as doing so complies with this policy.
119//!
120//! [`fmt`]: mod@fmt
121//! [`registry`]: mod@registry
122//! [`Collect`]: tracing_core::collect::Collect
123//! [collector]: tracing_core::collect::Collect
124//! [`EnvFilter`]: filter::EnvFilter
125//! [`tracing`]: https://crates.io/crates/tracing
126//! [`tracing-log`]: https://crates.io/crates/tracing-log
127//! [`smallvec`]: https://crates.io/crates/smallvec
128//! [`env_logger` crate]: https://crates.io/crates/env_logger
129//! [`parking_lot`]: https://crates.io/crates/parking_lot
130//! [`time` crate]: https://crates.io/crates/time
131//! [`liballoc`]: https://doc.rust-lang.org/alloc/index.html
132//! [`libstd`]: https://doc.rust-lang.org/std/index.html
133#![doc(
134 html_logo_url = "https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png",
135 html_favicon_url = "https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/favicon.ico",
136 issue_tracker_base_url = "https://github.com/tokio-rs/tracing/issues/"
137)]
138#![cfg_attr(
139 docsrs,
140 // Allows displaying cfgs/feature flags in the documentation.
141 feature(doc_cfg),
142 // Allows adding traits to RustDoc's list of "notable traits"
143 feature(doc_notable_trait),
144 // Fail the docs build if any intra-docs links are broken
145 deny(rustdoc::broken_intra_doc_links),
146)]
147#![warn(
148 missing_debug_implementations,
149 missing_docs,
150 rust_2018_idioms,
151 unreachable_pub,
152 bad_style,
153 dead_code,
154 improper_ctypes,
155 non_shorthand_field_patterns,
156 no_mangle_generic_items,
157 overflowing_literals,
158 path_statements,
159 patterns_in_fns_without_body,
160 private_interfaces,
161 private_bounds,
162 unconditional_recursion,
163 unused,
164 unused_allocation,
165 unused_comparisons,
166 unused_parens,
167 while_true
168)]
169// Using struct update syntax when a struct has no additional fields avoids
170// a potential source change if additional fields are added to the struct in the
171// future, reducing diff noise. Allow this even though clippy considers it
172// "needless".
173#![allow(clippy::needless_update)]
174#![cfg_attr(not(feature = "std"), no_std)]
175
176#[cfg(feature = "alloc")]
177extern crate alloc;
178
179#[macro_use]
180mod macros;
181
182pub mod field;
183pub mod filter;
184pub mod prelude;
185pub mod registry;
186
187pub mod subscribe;
188pub mod util;
189
190feature! {
191 #![feature = "std"]
192 pub mod reload;
193 pub(crate) mod sync;
194}
195
196feature! {
197 #![all(feature = "fmt", feature = "std")]
198 pub mod fmt;
199 pub use fmt::fmt;
200 pub use fmt::Subscriber as FmtSubscriber;
201}
202
203feature! {
204 #![all(feature = "env-filter", feature = "std")]
205 pub use filter::EnvFilter;
206}
207
208pub use subscribe::Subscribe;
209
210feature! {
211 #![all(feature = "registry", feature = "std")]
212 pub use registry::Registry;
213
214 /// Creates a default [`Registry`], a [`Collect`](tracing_core::Collect)
215 /// implementation which tracks per-span data and exposes it to
216 /// [`Subscribe`]s.
217 ///
218 /// Returns a default [`Registry`].
219 pub fn registry() -> Registry {
220 Registry::default()
221 }
222}
223
224mod sealed {
225 pub trait Sealed<A = ()> {}
226}