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