Find
C
ase-sensitive
R
egexp search
Path
mozilla-central
/
third_party
/
rust
/
tracing-subscriber
/
src
/
fmt
Navigation
Enable keyboard shortcuts
Name
Description
Size
fmt_layer.rs
58880
format
mod.rs
A `Subscriber` for formatting and logging `tracing` data. # Overview [`tracing`] is a framework for instrumenting Rust programs with context-aware, structured, event-based diagnostic information. This crate provides an implementation of the [`Subscriber`] trait that records `tracing`'s `Event`s and `Span`s by formatting them as text and logging them to stdout. # Usage First, add this to your `Cargo.toml` file: ```toml [dependencies] tracing-subscriber = "0.3" ``` *Compiler support: [requires `rustc` 1.63+][msrv]* [msrv]: super#supported-rust-versions Add the following to your executable to initialize the default subscriber: ```rust use tracing_subscriber; tracing_subscriber::fmt::init(); ``` ## Filtering Events with Environment Variables The default subscriber installed by `init` enables you to filter events at runtime using environment variables (using the [`EnvFilter`]). The filter syntax is a superset of the [`env_logger`] syntax. For example: - Setting `RUST_LOG=debug` enables all `Span`s and `Event`s set to the log level `DEBUG` or higher - Setting `RUST_LOG=my_crate=trace` enables `Span`s and `Event`s in `my_crate` at all log levels **Note**: This should **not** be called by libraries. Libraries should use [`tracing`] to publish `tracing` `Event`s. # Configuration You can configure a subscriber instead of using the defaults with the following functions: ### Subscriber The [`FmtSubscriber`] formats and records `tracing` events as line-oriented logs. You can create one by calling: ```rust let subscriber = tracing_subscriber::fmt() // ... add configuration .finish(); ``` You can find the configuration methods for [`FmtSubscriber`] in [`SubscriberBuilder`]. ## Formatters The output format used by the layer and subscriber in this module is represented by implementing the [`FormatEvent`] trait, and can be customized. This module provides a number of formatter implementations: * [`format::Full`]: The default formatter. This emits human-readable, single-line logs for each event that occurs, with the current span context displayed before the formatted representation of the event. See [here](format::Full#example-output) for sample output. * [`format::Compact`]: A variant of the default formatter, optimized for short line lengths. Fields from the current span context are appended to the fields of the formatted event. See [here](format::Compact#example-output) for sample output. * [`format::Pretty`]: Emits excessively pretty, multi-line logs, optimized for human readability. This is primarily intended to be used in local development and debugging, or for command-line applications, where automated analysis and compact storage of logs is less of a priority than readability and visual appeal. See [here](format::Pretty#example-output) for sample output. * [`format::Json`]: Outputs newline-delimited JSON logs. This is intended for production use with systems where structured logs are consumed as JSON by analysis and viewing tools. The JSON output is not optimized for human readability. See [here](format::Json#example-output) for sample output. ### Customizing Formatters The formatting of log lines for spans and events is controlled by two traits, [`FormatEvent`] and [`FormatFields`]. The [`FormatEvent`] trait determines the overall formatting of the log line, such as what information from the event's metadata and span context is included and in what order. The [`FormatFields`] trait determines how fields — both the event's fields and fields on spans — are formatted. The [`fmt::format`] module provides several types which implement these traits, many of which expose additional configuration options to customize their output. The [`format::Format`] type implements common configuration used by all the formatters provided in this crate, and can be used as a builder to set specific formatting settings. For example: ``` use tracing_subscriber::fmt; // Configure a custom event formatter let format = fmt::format() .with_level(false) // don't include levels in formatted output .with_target(false) // don't include targets .with_thread_ids(true) // include the thread ID of the current thread .with_thread_names(true) // include the name of the current thread .compact(); // use the `Compact` formatting style. // Create a `fmt` subscriber that uses our custom event format, and set it // as the default. tracing_subscriber::fmt() .event_format(format) .init(); ```
46056
time
writer.rs
Abstractions for creating [`io::Write`] instances. [`io::Write`]: std::io::Write
46048