• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! A `Subscriber` for formatting and logging `tracing` data.
2 //!
3 //! # Overview
4 //!
5 //! [`tracing`] is a framework for instrumenting Rust programs with context-aware,
6 //! structured, event-based diagnostic information. This crate provides an
7 //! implementation of the [`Subscriber`] trait that records `tracing`'s `Event`s
8 //! and `Span`s by formatting them as text and logging them to stdout.
9 //!
10 //! # Usage
11 //!
12 //! First, add this to your `Cargo.toml` file:
13 //!
14 //! ```toml
15 //! [dependencies]
16 //! tracing-subscriber = "0.3"
17 //! ```
18 //!
19 //! *Compiler support: [requires `rustc` 1.49+][msrv]*
20 //!
21 //! [msrv]: super#supported-rust-versions
22 //!
23 //! Add the following to your executable to initialize the default subscriber:
24 //! ```rust
25 //! use tracing_subscriber;
26 //!
27 //! tracing_subscriber::fmt::init();
28 //! ```
29 //!
30 //! ## Filtering Events with Environment Variables
31 //!
32 //! The default subscriber installed by `init` enables you to filter events
33 //! at runtime using environment variables (using the [`EnvFilter`]).
34 //!
35 //! The filter syntax is a superset of the [`env_logger`] syntax.
36 //!
37 //! For example:
38 //! - Setting `RUST_LOG=debug` enables all `Span`s and `Event`s
39 //!     set to the log level `DEBUG` or higher
40 //! - Setting `RUST_LOG=my_crate=trace` enables `Span`s and `Event`s
41 //!     in `my_crate` at all log levels
42 //!
43 //! **Note**: This should **not** be called by libraries. Libraries should use
44 //! [`tracing`] to publish `tracing` `Event`s.
45 //!
46 //! # Configuration
47 //!
48 //! You can configure a subscriber instead of using the defaults with
49 //! the following functions:
50 //!
51 //! ### Subscriber
52 //!
53 //! The [`FmtSubscriber`] formats and records `tracing` events as line-oriented logs.
54 //! You can create one by calling:
55 //!
56 //! ```rust
57 //! let subscriber = tracing_subscriber::fmt()
58 //!     // ... add configuration
59 //!     .finish();
60 //! ```
61 //!
62 //! You can find the configuration methods for [`FmtSubscriber`] in
63 //! [`SubscriberBuilder`].
64 //!
65 //! ## Formatters
66 //!
67 //! The output format used by the layer and subscriber in this module is
68 //! represented by implementing the [`FormatEvent`] trait, and can be
69 //! customized. This module provides a number of formatter implementations:
70 //!
71 //! * [`format::Full`]: The default formatter. This emits human-readable,
72 //!   single-line logs for each event that occurs, with the current span context
73 //!   displayed before the formatted representation of the event. See
74 //!   [here](format::Full#example-output) for sample output.
75 //!
76 //! * [`format::Compact`]: A variant of the default formatter, optimized for
77 //!   short line lengths. Fields from the current span context are appended to
78 //!   the fields of the formatted event. See
79 //!   [here](format::Compact#example-output) for sample output.
80 //!
81 //! * [`format::Pretty`]: Emits excessively pretty, multi-line logs, optimized
82 //!   for human readability. This is primarily intended to be used in local
83 //!   development and debugging, or for command-line applications, where
84 //!   automated analysis and compact storage of logs is less of a priority than
85 //!   readability and visual appeal. See [here](format::Pretty#example-output)
86 //!   for sample output.
87 //!
88 //! * [`format::Json`]: Outputs newline-delimited JSON logs. This is intended
89 //!   for production use with systems where structured logs are consumed as JSON
90 //!   by analysis and viewing tools. The JSON output is not optimized for human
91 //!   readability. See [here](format::Json#example-output) for sample output.
92 //!
93 //! ### Customizing Formatters
94 //!
95 //! The formatting of log lines for spans and events is controlled by two
96 //! traits, [`FormatEvent`] and [`FormatFields`]. The [`FormatEvent`] trait
97 //! determines the overall formatting of the log line, such as what information
98 //! from the event's metadata and span context is included and in what order.
99 //! The [`FormatFields`] trait determines how fields — both the event's
100 //! fields and fields on spans — are formatted.
101 //!
102 //! The [`fmt::format`] module provides several types which implement these traits,
103 //! many of which expose additional configuration options to customize their
104 //! output. The [`format::Format`] type implements common configuration used by
105 //! all the formatters provided in this crate, and can be used as a builder to
106 //! set specific formatting settings. For example:
107 //!
108 //! ```
109 //! use tracing_subscriber::fmt;
110 //!
111 //! // Configure a custom event formatter
112 //! let format = fmt::format()
113 //!    .with_level(false) // don't include levels in formatted output
114 //!    .with_target(false) // don't include targets
115 //!    .with_thread_ids(true) // include the thread ID of the current thread
116 //!    .with_thread_names(true) // include the name of the current thread
117 //!    .compact(); // use the `Compact` formatting style.
118 //!
119 //! // Create a `fmt` subscriber that uses our custom event format, and set it
120 //! // as the default.
121 //! tracing_subscriber::fmt()
122 //!     .event_format(format)
123 //!     .init();
124 //! ```
125 //!
126 //! However, if a specific output format is needed, other crates can
127 //! also implement [`FormatEvent`] and [`FormatFields`]. See those traits'
128 //! documentation for details on how to implement them.
129 //!
130 //! ## Filters
131 //!
132 //! If you want to filter the `tracing` `Events` based on environment
133 //! variables, you can use the [`EnvFilter`] as follows:
134 //!
135 //! ```rust
136 //! use tracing_subscriber::EnvFilter;
137 //!
138 //! let filter = EnvFilter::from_default_env();
139 //! ```
140 //!
141 //! As mentioned above, the [`EnvFilter`] allows `Span`s and `Event`s to
142 //! be filtered at runtime by setting the `RUST_LOG` environment variable.
143 //!
144 //! You can find the other available [`filter`]s in the documentation.
145 //!
146 //! ### Using Your Subscriber
147 //!
148 //! Finally, once you have configured your `Subscriber`, you need to
149 //! configure your executable to use it.
150 //!
151 //! A subscriber can be installed globally using:
152 //! ```rust
153 //! use tracing;
154 //! use tracing_subscriber::FmtSubscriber;
155 //!
156 //! let subscriber = FmtSubscriber::new();
157 //!
158 //! tracing::subscriber::set_global_default(subscriber)
159 //!     .map_err(|_err| eprintln!("Unable to set global default subscriber"));
160 //! // Note this will only fail if you try to set the global default
161 //! // subscriber multiple times
162 //! ```
163 //!
164 //! ### Composing Layers
165 //!
166 //! Composing an [`EnvFilter`] `Layer` and a [format `Layer`][super::fmt::Layer]:
167 //!
168 //! ```rust
169 //! use tracing_subscriber::{fmt, EnvFilter};
170 //! use tracing_subscriber::prelude::*;
171 //!
172 //! let fmt_layer = fmt::layer()
173 //!     .with_target(false);
174 //! let filter_layer = EnvFilter::try_from_default_env()
175 //!     .or_else(|_| EnvFilter::try_new("info"))
176 //!     .unwrap();
177 //!
178 //! tracing_subscriber::registry()
179 //!     .with(filter_layer)
180 //!     .with(fmt_layer)
181 //!     .init();
182 //! ```
183 //!
184 //! [`EnvFilter`]: super::filter::EnvFilter
185 //! [`env_logger`]: https://docs.rs/env_logger/
186 //! [`filter`]: super::filter
187 //! [`FmtSubscriber`]: Subscriber
188 //! [`Subscriber`]:
189 //!     https://docs.rs/tracing/latest/tracing/trait.Subscriber.html
190 //! [`tracing`]: https://crates.io/crates/tracing
191 //! [`fmt::format`]: mod@crate::fmt::format
192 use std::{any::TypeId, error::Error, io};
193 use tracing_core::{span, subscriber::Interest, Event, Metadata};
194 
195 mod fmt_layer;
196 #[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))]
197 pub mod format;
198 #[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))]
199 pub mod time;
200 #[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))]
201 pub mod writer;
202 
203 pub use fmt_layer::{FmtContext, FormattedFields, Layer};
204 
205 use crate::layer::Layer as _;
206 use crate::util::SubscriberInitExt;
207 use crate::{
208     filter::LevelFilter,
209     layer,
210     registry::{LookupSpan, Registry},
211 };
212 
213 #[doc(inline)]
214 pub use self::{
215     format::{format, FormatEvent, FormatFields},
216     time::time,
217     writer::{MakeWriter, TestWriter},
218 };
219 
220 /// A `Subscriber` that logs formatted representations of `tracing` events.
221 ///
222 /// This consists of an inner `Formatter` wrapped in a layer that performs filtering.
223 #[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))]
224 #[derive(Debug)]
225 pub struct Subscriber<
226     N = format::DefaultFields,
227     E = format::Format<format::Full>,
228     F = LevelFilter,
229     W = fn() -> io::Stdout,
230 > {
231     inner: layer::Layered<F, Formatter<N, E, W>>,
232 }
233 
234 /// A `Subscriber` that logs formatted representations of `tracing` events.
235 /// This type only logs formatted events; it does not perform any filtering.
236 #[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))]
237 pub type Formatter<
238     N = format::DefaultFields,
239     E = format::Format<format::Full>,
240     W = fn() -> io::Stdout,
241 > = layer::Layered<fmt_layer::Layer<Registry, N, E, W>, Registry>;
242 
243 /// Configures and constructs `Subscriber`s.
244 #[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))]
245 #[derive(Debug)]
246 #[must_use]
247 pub struct SubscriberBuilder<
248     N = format::DefaultFields,
249     E = format::Format<format::Full>,
250     F = LevelFilter,
251     W = fn() -> io::Stdout,
252 > {
253     filter: F,
254     inner: Layer<Registry, N, E, W>,
255 }
256 
257 /// Returns a new [`SubscriberBuilder`] for configuring a [formatting subscriber].
258 ///
259 /// This is essentially shorthand for [`SubscriberBuilder::default()]`.
260 ///
261 /// # Examples
262 ///
263 /// Using [`init`] to set the default subscriber:
264 ///
265 /// ```rust
266 /// tracing_subscriber::fmt().init();
267 /// ```
268 ///
269 /// Configuring the output format:
270 ///
271 /// ```rust
272 ///
273 /// tracing_subscriber::fmt()
274 ///     // Configure formatting settings.
275 ///     .with_target(false)
276 ///     .with_timer(tracing_subscriber::fmt::time::uptime())
277 ///     .with_level(true)
278 ///     // Set the subscriber as the default.
279 ///     .init();
280 /// ```
281 ///
282 /// [`try_init`] returns an error if the default subscriber could not be set:
283 ///
284 /// ```rust
285 /// use std::error::Error;
286 ///
287 /// fn init_subscriber() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
288 ///     tracing_subscriber::fmt()
289 ///         // Configure the subscriber to emit logs in JSON format.
290 ///         .json()
291 ///         // Configure the subscriber to flatten event fields in the output JSON objects.
292 ///         .flatten_event(true)
293 ///         // Set the subscriber as the default, returning an error if this fails.
294 ///         .try_init()?;
295 ///
296 ///     Ok(())
297 /// }
298 /// ```
299 ///
300 /// Rather than setting the subscriber as the default, [`finish`] _returns_ the
301 /// constructed subscriber, which may then be passed to other functions:
302 ///
303 /// ```rust
304 /// let subscriber = tracing_subscriber::fmt()
305 ///     .with_max_level(tracing::Level::DEBUG)
306 ///     .compact()
307 ///     .finish();
308 ///
309 /// tracing::subscriber::with_default(subscriber, || {
310 ///     // the subscriber will only be set as the default
311 ///     // inside this closure...
312 /// })
313 /// ```
314 ///
315 /// [formatting subscriber]: Subscriber
316 /// [`SubscriberBuilder::default()`]: SubscriberBuilder::default
317 /// [`init`]: SubscriberBuilder::init()
318 /// [`try_init`]: SubscriberBuilder::try_init()
319 /// [`finish`]: SubscriberBuilder::finish()
320 #[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))]
fmt() -> SubscriberBuilder321 pub fn fmt() -> SubscriberBuilder {
322     SubscriberBuilder::default()
323 }
324 
325 /// Returns a new [formatting layer] that can be [composed] with other layers to
326 /// construct a [`Subscriber`].
327 ///
328 /// This is a shorthand for the equivalent [`Layer::default()`] function.
329 ///
330 /// [formatting layer]: Layer
331 /// [composed]: crate::layer
332 /// [`Layer::default()`]: Layer::default
333 #[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))]
layer<S>() -> Layer<S>334 pub fn layer<S>() -> Layer<S> {
335     Layer::default()
336 }
337 
338 impl Subscriber {
339     /// The maximum [verbosity level] that is enabled by a `Subscriber` by
340     /// default.
341     ///
342     /// This can be overridden with the [`SubscriberBuilder::with_max_level`] method.
343     ///
344     /// [verbosity level]: tracing_core::Level
345     /// [`SubscriberBuilder::with_max_level`]: SubscriberBuilder::with_max_level
346     pub const DEFAULT_MAX_LEVEL: LevelFilter = LevelFilter::INFO;
347 
348     /// Returns a new `SubscriberBuilder` for configuring a format subscriber.
builder() -> SubscriberBuilder349     pub fn builder() -> SubscriberBuilder {
350         SubscriberBuilder::default()
351     }
352 
353     /// Returns a new format subscriber with the default configuration.
new() -> Self354     pub fn new() -> Self {
355         Default::default()
356     }
357 }
358 
359 impl Default for Subscriber {
default() -> Self360     fn default() -> Self {
361         SubscriberBuilder::default().finish()
362     }
363 }
364 
365 // === impl Subscriber ===
366 
367 impl<N, E, F, W> tracing_core::Subscriber for Subscriber<N, E, F, W>
368 where
369     N: for<'writer> FormatFields<'writer> + 'static,
370     E: FormatEvent<Registry, N> + 'static,
371     F: layer::Layer<Formatter<N, E, W>> + 'static,
372     W: for<'writer> MakeWriter<'writer> + 'static,
373     layer::Layered<F, Formatter<N, E, W>>: tracing_core::Subscriber,
374     fmt_layer::Layer<Registry, N, E, W>: layer::Layer<Registry>,
375 {
376     #[inline]
register_callsite(&self, meta: &'static Metadata<'static>) -> Interest377     fn register_callsite(&self, meta: &'static Metadata<'static>) -> Interest {
378         self.inner.register_callsite(meta)
379     }
380 
381     #[inline]
enabled(&self, meta: &Metadata<'_>) -> bool382     fn enabled(&self, meta: &Metadata<'_>) -> bool {
383         self.inner.enabled(meta)
384     }
385 
386     #[inline]
new_span(&self, attrs: &span::Attributes<'_>) -> span::Id387     fn new_span(&self, attrs: &span::Attributes<'_>) -> span::Id {
388         self.inner.new_span(attrs)
389     }
390 
391     #[inline]
record(&self, span: &span::Id, values: &span::Record<'_>)392     fn record(&self, span: &span::Id, values: &span::Record<'_>) {
393         self.inner.record(span, values)
394     }
395 
396     #[inline]
record_follows_from(&self, span: &span::Id, follows: &span::Id)397     fn record_follows_from(&self, span: &span::Id, follows: &span::Id) {
398         self.inner.record_follows_from(span, follows)
399     }
400 
401     #[inline]
event_enabled(&self, event: &Event<'_>) -> bool402     fn event_enabled(&self, event: &Event<'_>) -> bool {
403         self.inner.event_enabled(event)
404     }
405 
406     #[inline]
event(&self, event: &Event<'_>)407     fn event(&self, event: &Event<'_>) {
408         self.inner.event(event);
409     }
410 
411     #[inline]
enter(&self, id: &span::Id)412     fn enter(&self, id: &span::Id) {
413         // TODO: add on_enter hook
414         self.inner.enter(id);
415     }
416 
417     #[inline]
exit(&self, id: &span::Id)418     fn exit(&self, id: &span::Id) {
419         self.inner.exit(id);
420     }
421 
422     #[inline]
current_span(&self) -> span::Current423     fn current_span(&self) -> span::Current {
424         self.inner.current_span()
425     }
426 
427     #[inline]
clone_span(&self, id: &span::Id) -> span::Id428     fn clone_span(&self, id: &span::Id) -> span::Id {
429         self.inner.clone_span(id)
430     }
431 
432     #[inline]
try_close(&self, id: span::Id) -> bool433     fn try_close(&self, id: span::Id) -> bool {
434         self.inner.try_close(id)
435     }
436 
437     #[inline]
max_level_hint(&self) -> Option<tracing_core::LevelFilter>438     fn max_level_hint(&self) -> Option<tracing_core::LevelFilter> {
439         self.inner.max_level_hint()
440     }
441 
downcast_raw(&self, id: TypeId) -> Option<*const ()>442     unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> {
443         if id == TypeId::of::<Self>() {
444             Some(self as *const Self as *const ())
445         } else {
446             self.inner.downcast_raw(id)
447         }
448     }
449 }
450 
451 impl<'a, N, E, F, W> LookupSpan<'a> for Subscriber<N, E, F, W>
452 where
453     layer::Layered<F, Formatter<N, E, W>>: LookupSpan<'a>,
454 {
455     type Data = <layer::Layered<F, Formatter<N, E, W>> as LookupSpan<'a>>::Data;
456 
span_data(&'a self, id: &span::Id) -> Option<Self::Data>457     fn span_data(&'a self, id: &span::Id) -> Option<Self::Data> {
458         self.inner.span_data(id)
459     }
460 }
461 
462 // ===== impl SubscriberBuilder =====
463 
464 impl Default for SubscriberBuilder {
default() -> Self465     fn default() -> Self {
466         SubscriberBuilder {
467             filter: Subscriber::DEFAULT_MAX_LEVEL,
468             inner: Default::default(),
469         }
470         .log_internal_errors(true)
471     }
472 }
473 
474 impl<N, E, F, W> SubscriberBuilder<N, E, F, W>
475 where
476     N: for<'writer> FormatFields<'writer> + 'static,
477     E: FormatEvent<Registry, N> + 'static,
478     W: for<'writer> MakeWriter<'writer> + 'static,
479     F: layer::Layer<Formatter<N, E, W>> + Send + Sync + 'static,
480     fmt_layer::Layer<Registry, N, E, W>: layer::Layer<Registry> + Send + Sync + 'static,
481 {
482     /// Finish the builder, returning a new `FmtSubscriber`.
finish(self) -> Subscriber<N, E, F, W>483     pub fn finish(self) -> Subscriber<N, E, F, W> {
484         let subscriber = self.inner.with_subscriber(Registry::default());
485         Subscriber {
486             inner: self.filter.with_subscriber(subscriber),
487         }
488     }
489 
490     /// Install this Subscriber as the global default if one is
491     /// not already set.
492     ///
493     /// If the `tracing-log` feature is enabled, this will also install
494     /// the LogTracer to convert `Log` records into `tracing` `Event`s.
495     ///
496     /// # Errors
497     /// Returns an Error if the initialization was unsuccessful, likely
498     /// because a global subscriber was already installed by another
499     /// call to `try_init`.
try_init(self) -> Result<(), Box<dyn Error + Send + Sync + 'static>>500     pub fn try_init(self) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
501         use crate::util::SubscriberInitExt;
502         self.finish().try_init()?;
503 
504         Ok(())
505     }
506 
507     /// Install this Subscriber as the global default.
508     ///
509     /// If the `tracing-log` feature is enabled, this will also install
510     /// the LogTracer to convert `Log` records into `tracing` `Event`s.
511     ///
512     /// # Panics
513     /// Panics if the initialization was unsuccessful, likely because a
514     /// global subscriber was already installed by another call to `try_init`.
init(self)515     pub fn init(self) {
516         self.try_init()
517             .expect("Unable to install global subscriber")
518     }
519 }
520 
521 impl<N, E, F, W> From<SubscriberBuilder<N, E, F, W>> for tracing_core::Dispatch
522 where
523     N: for<'writer> FormatFields<'writer> + 'static,
524     E: FormatEvent<Registry, N> + 'static,
525     W: for<'writer> MakeWriter<'writer> + 'static,
526     F: layer::Layer<Formatter<N, E, W>> + Send + Sync + 'static,
527     fmt_layer::Layer<Registry, N, E, W>: layer::Layer<Registry> + Send + Sync + 'static,
528 {
from(builder: SubscriberBuilder<N, E, F, W>) -> tracing_core::Dispatch529     fn from(builder: SubscriberBuilder<N, E, F, W>) -> tracing_core::Dispatch {
530         tracing_core::Dispatch::new(builder.finish())
531     }
532 }
533 
534 impl<N, L, T, F, W> SubscriberBuilder<N, format::Format<L, T>, F, W>
535 where
536     N: for<'writer> FormatFields<'writer> + 'static,
537 {
538     /// Use the given [`timer`] for log message timestamps.
539     ///
540     /// See the [`time` module] for the provided timer implementations.
541     ///
542     /// Note that using the `"time`"" feature flag enables the
543     /// additional time formatters [`UtcTime`] and [`LocalTime`], which use the
544     /// [`time` crate] to provide more sophisticated timestamp formatting
545     /// options.
546     ///
547     /// [`timer`]: time::FormatTime
548     /// [`time` module]: mod@time
549     /// [`UtcTime`]: time::UtcTime
550     /// [`LocalTime`]: time::LocalTime
551     /// [`time` crate]: https://docs.rs/time/0.3
with_timer<T2>(self, timer: T2) -> SubscriberBuilder<N, format::Format<L, T2>, F, W>552     pub fn with_timer<T2>(self, timer: T2) -> SubscriberBuilder<N, format::Format<L, T2>, F, W> {
553         SubscriberBuilder {
554             filter: self.filter,
555             inner: self.inner.with_timer(timer),
556         }
557     }
558 
559     /// Do not emit timestamps with log messages.
without_time(self) -> SubscriberBuilder<N, format::Format<L, ()>, F, W>560     pub fn without_time(self) -> SubscriberBuilder<N, format::Format<L, ()>, F, W> {
561         SubscriberBuilder {
562             filter: self.filter,
563             inner: self.inner.without_time(),
564         }
565     }
566 
567     /// Configures how synthesized events are emitted at points in the [span
568     /// lifecycle][lifecycle].
569     ///
570     /// The following options are available:
571     ///
572     /// - `FmtSpan::NONE`: No events will be synthesized when spans are
573     ///    created, entered, exited, or closed. Data from spans will still be
574     ///    included as the context for formatted events. This is the default.
575     /// - `FmtSpan::NEW`: An event will be synthesized when spans are created.
576     /// - `FmtSpan::ENTER`: An event will be synthesized when spans are entered.
577     /// - `FmtSpan::EXIT`: An event will be synthesized when spans are exited.
578     /// - `FmtSpan::CLOSE`: An event will be synthesized when a span closes. If
579     ///    [timestamps are enabled][time] for this formatter, the generated
580     ///    event will contain fields with the span's _busy time_ (the total
581     ///    time for which it was entered) and _idle time_ (the total time that
582     ///    the span existed but was not entered).
583     /// - `FmtSpan::ACTIVE`: An event will be synthesized when spans are entered
584     ///    or exited.
585     /// - `FmtSpan::FULL`: Events will be synthesized whenever a span is
586     ///    created, entered, exited, or closed. If timestamps are enabled, the
587     ///    close event will contain the span's busy and idle time, as
588     ///    described above.
589     ///
590     /// The options can be enabled in any combination. For instance, the following
591     /// will synthesize events whenever spans are created and closed:
592     ///
593     /// ```rust
594     /// use tracing_subscriber::fmt::format::FmtSpan;
595     /// use tracing_subscriber::fmt;
596     ///
597     /// let subscriber = fmt()
598     ///     .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
599     ///     .finish();
600     /// ```
601     ///
602     /// Note that the generated events will only be part of the log output by
603     /// this formatter; they will not be recorded by other `Subscriber`s or by
604     /// `Layer`s added to this subscriber.
605     ///
606     /// [lifecycle]: https://docs.rs/tracing/latest/tracing/span/index.html#the-span-lifecycle
607     /// [time]: SubscriberBuilder::without_time()
with_span_events(self, kind: format::FmtSpan) -> Self608     pub fn with_span_events(self, kind: format::FmtSpan) -> Self {
609         SubscriberBuilder {
610             inner: self.inner.with_span_events(kind),
611             ..self
612         }
613     }
614 
615     /// Enable ANSI encoding for formatted events.
616     #[cfg(feature = "ansi")]
617     #[cfg_attr(docsrs, doc(cfg(feature = "ansi")))]
with_ansi(self, ansi: bool) -> SubscriberBuilder<N, format::Format<L, T>, F, W>618     pub fn with_ansi(self, ansi: bool) -> SubscriberBuilder<N, format::Format<L, T>, F, W> {
619         SubscriberBuilder {
620             inner: self.inner.with_ansi(ansi),
621             ..self
622         }
623     }
624 
625     /// Sets whether to write errors from [`FormatEvent`] to the writer.
626     /// Defaults to true.
627     ///
628     /// By default, `fmt::Layer` will write any `FormatEvent`-internal errors to
629     /// the writer. These errors are unlikely and will only occur if there is a
630     /// bug in the `FormatEvent` implementation or its dependencies.
631     ///
632     /// If writing to the writer fails, the error message is printed to stderr
633     /// as a fallback.
634     ///
635     /// [`FormatEvent`]: crate::fmt::FormatEvent
log_internal_errors( self, log_internal_errors: bool, ) -> SubscriberBuilder<N, format::Format<L, T>, F, W>636     pub fn log_internal_errors(
637         self,
638         log_internal_errors: bool,
639     ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> {
640         SubscriberBuilder {
641             inner: self.inner.log_internal_errors(log_internal_errors),
642             ..self
643         }
644     }
645 
646     /// Sets whether or not an event's target is displayed.
with_target( self, display_target: bool, ) -> SubscriberBuilder<N, format::Format<L, T>, F, W>647     pub fn with_target(
648         self,
649         display_target: bool,
650     ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> {
651         SubscriberBuilder {
652             inner: self.inner.with_target(display_target),
653             ..self
654         }
655     }
656 
657     /// Sets whether or not an event's [source code file path][file] is
658     /// displayed.
659     ///
660     /// [file]: tracing_core::Metadata::file
with_file( self, display_filename: bool, ) -> SubscriberBuilder<N, format::Format<L, T>, F, W>661     pub fn with_file(
662         self,
663         display_filename: bool,
664     ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> {
665         SubscriberBuilder {
666             inner: self.inner.with_file(display_filename),
667             ..self
668         }
669     }
670 
671     /// Sets whether or not an event's [source code line number][line] is
672     /// displayed.
673     ///
674     /// [line]: tracing_core::Metadata::line
with_line_number( self, display_line_number: bool, ) -> SubscriberBuilder<N, format::Format<L, T>, F, W>675     pub fn with_line_number(
676         self,
677         display_line_number: bool,
678     ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> {
679         SubscriberBuilder {
680             inner: self.inner.with_line_number(display_line_number),
681             ..self
682         }
683     }
684 
685     /// Sets whether or not an event's level is displayed.
with_level( self, display_level: bool, ) -> SubscriberBuilder<N, format::Format<L, T>, F, W>686     pub fn with_level(
687         self,
688         display_level: bool,
689     ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> {
690         SubscriberBuilder {
691             inner: self.inner.with_level(display_level),
692             ..self
693         }
694     }
695 
696     /// Sets whether or not the [name] of the current thread is displayed
697     /// when formatting events.
698     ///
699     /// [name]: std::thread#naming-threads
with_thread_names( self, display_thread_names: bool, ) -> SubscriberBuilder<N, format::Format<L, T>, F, W>700     pub fn with_thread_names(
701         self,
702         display_thread_names: bool,
703     ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> {
704         SubscriberBuilder {
705             inner: self.inner.with_thread_names(display_thread_names),
706             ..self
707         }
708     }
709 
710     /// Sets whether or not the [thread ID] of the current thread is displayed
711     /// when formatting events.
712     ///
713     /// [thread ID]: std::thread::ThreadId
with_thread_ids( self, display_thread_ids: bool, ) -> SubscriberBuilder<N, format::Format<L, T>, F, W>714     pub fn with_thread_ids(
715         self,
716         display_thread_ids: bool,
717     ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> {
718         SubscriberBuilder {
719             inner: self.inner.with_thread_ids(display_thread_ids),
720             ..self
721         }
722     }
723 
724     /// Sets the subscriber being built to use a less verbose formatter.
725     ///
726     /// See [`format::Compact`].
compact(self) -> SubscriberBuilder<N, format::Format<format::Compact, T>, F, W> where N: for<'writer> FormatFields<'writer> + 'static,727     pub fn compact(self) -> SubscriberBuilder<N, format::Format<format::Compact, T>, F, W>
728     where
729         N: for<'writer> FormatFields<'writer> + 'static,
730     {
731         SubscriberBuilder {
732             filter: self.filter,
733             inner: self.inner.compact(),
734         }
735     }
736 
737     /// Sets the subscriber being built to use an [excessively pretty, human-readable formatter](crate::fmt::format::Pretty).
738     #[cfg(feature = "ansi")]
739     #[cfg_attr(docsrs, doc(cfg(feature = "ansi")))]
pretty( self, ) -> SubscriberBuilder<format::Pretty, format::Format<format::Pretty, T>, F, W>740     pub fn pretty(
741         self,
742     ) -> SubscriberBuilder<format::Pretty, format::Format<format::Pretty, T>, F, W> {
743         SubscriberBuilder {
744             filter: self.filter,
745             inner: self.inner.pretty(),
746         }
747     }
748 
749     /// Sets the subscriber being built to use a JSON formatter.
750     ///
751     /// See [`format::Json`][super::fmt::format::Json]
752     #[cfg(feature = "json")]
753     #[cfg_attr(docsrs, doc(cfg(feature = "json")))]
json( self, ) -> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> where N: for<'writer> FormatFields<'writer> + 'static,754     pub fn json(
755         self,
756     ) -> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W>
757     where
758         N: for<'writer> FormatFields<'writer> + 'static,
759     {
760         SubscriberBuilder {
761             filter: self.filter,
762             inner: self.inner.json(),
763         }
764     }
765 }
766 
767 #[cfg(feature = "json")]
768 #[cfg_attr(docsrs, doc(cfg(feature = "json")))]
769 impl<T, F, W> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> {
770     /// Sets the json subscriber being built to flatten event metadata.
771     ///
772     /// See [`format::Json`][super::fmt::format::Json]
flatten_event( self, flatten_event: bool, ) -> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W>773     pub fn flatten_event(
774         self,
775         flatten_event: bool,
776     ) -> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> {
777         SubscriberBuilder {
778             filter: self.filter,
779             inner: self.inner.flatten_event(flatten_event),
780         }
781     }
782 
783     /// Sets whether or not the JSON subscriber being built will include the current span
784     /// in formatted events.
785     ///
786     /// See [`format::Json`][super::fmt::format::Json]
with_current_span( self, display_current_span: bool, ) -> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W>787     pub fn with_current_span(
788         self,
789         display_current_span: bool,
790     ) -> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> {
791         SubscriberBuilder {
792             filter: self.filter,
793             inner: self.inner.with_current_span(display_current_span),
794         }
795     }
796 
797     /// Sets whether or not the JSON subscriber being built will include a list (from
798     /// root to leaf) of all currently entered spans in formatted events.
799     ///
800     /// See [`format::Json`][super::fmt::format::Json]
with_span_list( self, display_span_list: bool, ) -> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W>801     pub fn with_span_list(
802         self,
803         display_span_list: bool,
804     ) -> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> {
805         SubscriberBuilder {
806             filter: self.filter,
807             inner: self.inner.with_span_list(display_span_list),
808         }
809     }
810 }
811 
812 #[cfg(feature = "env-filter")]
813 #[cfg_attr(docsrs, doc(cfg(feature = "env-filter")))]
814 impl<N, E, W> SubscriberBuilder<N, E, crate::EnvFilter, W>
815 where
816     Formatter<N, E, W>: tracing_core::Subscriber + 'static,
817 {
818     /// Configures the subscriber being built to allow filter reloading at
819     /// runtime.
with_filter_reloading( self, ) -> SubscriberBuilder<N, E, crate::reload::Layer<crate::EnvFilter, Formatter<N, E, W>>, W>820     pub fn with_filter_reloading(
821         self,
822     ) -> SubscriberBuilder<N, E, crate::reload::Layer<crate::EnvFilter, Formatter<N, E, W>>, W>
823     {
824         let (filter, _) = crate::reload::Layer::new(self.filter);
825         SubscriberBuilder {
826             filter,
827             inner: self.inner,
828         }
829     }
830 }
831 
832 #[cfg(feature = "env-filter")]
833 #[cfg_attr(docsrs, doc(cfg(feature = "env-filter")))]
834 impl<N, E, W> SubscriberBuilder<N, E, crate::reload::Layer<crate::EnvFilter, Formatter<N, E, W>>, W>
835 where
836     Formatter<N, E, W>: tracing_core::Subscriber + 'static,
837 {
838     /// Returns a `Handle` that may be used to reload the constructed subscriber's
839     /// filter.
reload_handle(&self) -> crate::reload::Handle<crate::EnvFilter, Formatter<N, E, W>>840     pub fn reload_handle(&self) -> crate::reload::Handle<crate::EnvFilter, Formatter<N, E, W>> {
841         self.filter.handle()
842     }
843 }
844 
845 impl<N, E, F, W> SubscriberBuilder<N, E, F, W> {
846     /// Sets the field formatter that the subscriber being built will use to record
847     /// fields.
848     ///
849     /// For example:
850     /// ```rust
851     /// use tracing_subscriber::fmt::format;
852     /// use tracing_subscriber::prelude::*;
853     ///
854     /// let formatter =
855     ///     // Construct a custom formatter for `Debug` fields
856     ///     format::debug_fn(|writer, field, value| write!(writer, "{}: {:?}", field, value))
857     ///         // Use the `tracing_subscriber::MakeFmtExt` trait to wrap the
858     ///         // formatter so that a delimiter is added between fields.
859     ///         .delimited(", ");
860     ///
861     /// let subscriber = tracing_subscriber::fmt()
862     ///     .fmt_fields(formatter)
863     ///     .finish();
864     /// # drop(subscriber)
865     /// ```
fmt_fields<N2>(self, fmt_fields: N2) -> SubscriberBuilder<N2, E, F, W> where N2: for<'writer> FormatFields<'writer> + 'static,866     pub fn fmt_fields<N2>(self, fmt_fields: N2) -> SubscriberBuilder<N2, E, F, W>
867     where
868         N2: for<'writer> FormatFields<'writer> + 'static,
869     {
870         SubscriberBuilder {
871             filter: self.filter,
872             inner: self.inner.fmt_fields(fmt_fields),
873         }
874     }
875 
876     /// Sets the [`EnvFilter`] that the subscriber will use to determine if
877     /// a span or event is enabled.
878     ///
879     /// Note that this method requires the "env-filter" feature flag to be enabled.
880     ///
881     /// If a filter was previously set, or a maximum level was set by the
882     /// [`with_max_level`] method, that value is replaced by the new filter.
883     ///
884     /// # Examples
885     ///
886     /// Setting a filter based on the value of the `RUST_LOG` environment
887     /// variable:
888     /// ```rust
889     /// use tracing_subscriber::{fmt, EnvFilter};
890     ///
891     /// fmt()
892     ///     .with_env_filter(EnvFilter::from_default_env())
893     ///     .init();
894     /// ```
895     ///
896     /// Setting a filter based on a pre-set filter directive string:
897     /// ```rust
898     /// use tracing_subscriber::fmt;
899     ///
900     /// fmt()
901     ///     .with_env_filter("my_crate=info,my_crate::my_mod=debug,[my_span]=trace")
902     ///     .init();
903     /// ```
904     ///
905     /// Adding additional directives to a filter constructed from an env var:
906     /// ```rust
907     /// use tracing_subscriber::{fmt, filter::{EnvFilter, LevelFilter}};
908     ///
909     /// # fn filter() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
910     /// let filter = EnvFilter::try_from_env("MY_CUSTOM_FILTER_ENV_VAR")?
911     ///     // Set the base level when not matched by other directives to WARN.
912     ///     .add_directive(LevelFilter::WARN.into())
913     ///     // Set the max level for `my_crate::my_mod` to DEBUG, overriding
914     ///     // any directives parsed from the env variable.
915     ///     .add_directive("my_crate::my_mod=debug".parse()?);
916     ///
917     /// fmt()
918     ///     .with_env_filter(filter)
919     ///     .try_init()?;
920     /// # Ok(())}
921     /// ```
922     /// [`EnvFilter`]: super::filter::EnvFilter
923     /// [`with_max_level`]: SubscriberBuilder::with_max_level()
924     #[cfg(feature = "env-filter")]
925     #[cfg_attr(docsrs, doc(cfg(feature = "env-filter")))]
with_env_filter( self, filter: impl Into<crate::EnvFilter>, ) -> SubscriberBuilder<N, E, crate::EnvFilter, W> where Formatter<N, E, W>: tracing_core::Subscriber + 'static,926     pub fn with_env_filter(
927         self,
928         filter: impl Into<crate::EnvFilter>,
929     ) -> SubscriberBuilder<N, E, crate::EnvFilter, W>
930     where
931         Formatter<N, E, W>: tracing_core::Subscriber + 'static,
932     {
933         let filter = filter.into();
934         SubscriberBuilder {
935             filter,
936             inner: self.inner,
937         }
938     }
939 
940     /// Sets the maximum [verbosity level] that will be enabled by the
941     /// subscriber.
942     ///
943     /// If the max level has already been set, or a [`EnvFilter`] was added by
944     /// [`with_env_filter`], this replaces that configuration with the new
945     /// maximum level.
946     ///
947     /// # Examples
948     ///
949     /// Enable up to the `DEBUG` verbosity level:
950     /// ```rust
951     /// use tracing_subscriber::fmt;
952     /// use tracing::Level;
953     ///
954     /// fmt()
955     ///     .with_max_level(Level::DEBUG)
956     ///     .init();
957     /// ```
958     /// This subscriber won't record any spans or events!
959     /// ```rust
960     /// use tracing_subscriber::{fmt, filter::LevelFilter};
961     ///
962     /// let subscriber = fmt()
963     ///     .with_max_level(LevelFilter::OFF)
964     ///     .finish();
965     /// ```
966     /// [verbosity level]: tracing_core::Level
967     /// [`EnvFilter`]: struct@crate::filter::EnvFilter
968     /// [`with_env_filter`]: fn@Self::with_env_filter
with_max_level( self, filter: impl Into<LevelFilter>, ) -> SubscriberBuilder<N, E, LevelFilter, W>969     pub fn with_max_level(
970         self,
971         filter: impl Into<LevelFilter>,
972     ) -> SubscriberBuilder<N, E, LevelFilter, W> {
973         let filter = filter.into();
974         SubscriberBuilder {
975             filter,
976             inner: self.inner,
977         }
978     }
979 
980     /// Sets the [event formatter][`FormatEvent`] that the subscriber being built
981     /// will use to format events that occur.
982     ///
983     /// The event formatter may be any type implementing the [`FormatEvent`]
984     /// trait, which is implemented for all functions taking a [`FmtContext`], a
985     /// [`Writer`], and an [`Event`].
986     ///
987     /// # Examples
988     ///
989     /// Setting a type implementing [`FormatEvent`] as the formatter:
990     ///
991     /// ```rust
992     /// use tracing_subscriber::fmt::format;
993     ///
994     /// let subscriber = tracing_subscriber::fmt()
995     ///     .event_format(format().compact())
996     ///     .finish();
997     /// ```
998     ///
999     /// [`Writer`]: struct@self::format::Writer
event_format<E2>(self, fmt_event: E2) -> SubscriberBuilder<N, E2, F, W> where E2: FormatEvent<Registry, N> + 'static, N: for<'writer> FormatFields<'writer> + 'static, W: for<'writer> MakeWriter<'writer> + 'static,1000     pub fn event_format<E2>(self, fmt_event: E2) -> SubscriberBuilder<N, E2, F, W>
1001     where
1002         E2: FormatEvent<Registry, N> + 'static,
1003         N: for<'writer> FormatFields<'writer> + 'static,
1004         W: for<'writer> MakeWriter<'writer> + 'static,
1005     {
1006         SubscriberBuilder {
1007             filter: self.filter,
1008             inner: self.inner.event_format(fmt_event),
1009         }
1010     }
1011 
1012     /// Sets the [`MakeWriter`] that the subscriber being built will use to write events.
1013     ///
1014     /// # Examples
1015     ///
1016     /// Using `stderr` rather than `stdout`:
1017     ///
1018     /// ```rust
1019     /// use tracing_subscriber::fmt;
1020     /// use std::io;
1021     ///
1022     /// fmt()
1023     ///     .with_writer(io::stderr)
1024     ///     .init();
1025     /// ```
with_writer<W2>(self, make_writer: W2) -> SubscriberBuilder<N, E, F, W2> where W2: for<'writer> MakeWriter<'writer> + 'static,1026     pub fn with_writer<W2>(self, make_writer: W2) -> SubscriberBuilder<N, E, F, W2>
1027     where
1028         W2: for<'writer> MakeWriter<'writer> + 'static,
1029     {
1030         SubscriberBuilder {
1031             filter: self.filter,
1032             inner: self.inner.with_writer(make_writer),
1033         }
1034     }
1035 
1036     /// Configures the subscriber to support [`libtest`'s output capturing][capturing] when used in
1037     /// unit tests.
1038     ///
1039     /// See [`TestWriter`] for additional details.
1040     ///
1041     /// # Examples
1042     ///
1043     /// Using [`TestWriter`] to let `cargo test` capture test output. Note that we do not install it
1044     /// globally as it may cause conflicts.
1045     ///
1046     /// ```rust
1047     /// use tracing_subscriber::fmt;
1048     /// use tracing::subscriber;
1049     ///
1050     /// subscriber::set_default(
1051     ///     fmt()
1052     ///         .with_test_writer()
1053     ///         .finish()
1054     /// );
1055     /// ```
1056     ///
1057     /// [capturing]:
1058     /// https://doc.rust-lang.org/book/ch11-02-running-tests.html#showing-function-output
1059     /// [`TestWriter`]: writer::TestWriter
with_test_writer(self) -> SubscriberBuilder<N, E, F, TestWriter>1060     pub fn with_test_writer(self) -> SubscriberBuilder<N, E, F, TestWriter> {
1061         SubscriberBuilder {
1062             filter: self.filter,
1063             inner: self.inner.with_writer(TestWriter::default()),
1064         }
1065     }
1066 
1067     /// Updates the event formatter by applying a function to the existing event formatter.
1068     ///
1069     /// This sets the event formatter that the subscriber being built will use to record fields.
1070     ///
1071     /// # Examples
1072     ///
1073     /// Updating an event formatter:
1074     ///
1075     /// ```rust
1076     /// let subscriber = tracing_subscriber::fmt()
1077     ///     .map_event_format(|e| e.compact())
1078     ///     .finish();
1079     /// ```
map_event_format<E2>(self, f: impl FnOnce(E) -> E2) -> SubscriberBuilder<N, E2, F, W> where E2: FormatEvent<Registry, N> + 'static, N: for<'writer> FormatFields<'writer> + 'static, W: for<'writer> MakeWriter<'writer> + 'static,1080     pub fn map_event_format<E2>(self, f: impl FnOnce(E) -> E2) -> SubscriberBuilder<N, E2, F, W>
1081     where
1082         E2: FormatEvent<Registry, N> + 'static,
1083         N: for<'writer> FormatFields<'writer> + 'static,
1084         W: for<'writer> MakeWriter<'writer> + 'static,
1085     {
1086         SubscriberBuilder {
1087             filter: self.filter,
1088             inner: self.inner.map_event_format(f),
1089         }
1090     }
1091 
1092     /// Updates the field formatter by applying a function to the existing field formatter.
1093     ///
1094     /// This sets the field formatter that the subscriber being built will use to record fields.
1095     ///
1096     /// # Examples
1097     ///
1098     /// Updating a field formatter:
1099     ///
1100     /// ```rust
1101     /// use tracing_subscriber::field::MakeExt;
1102     /// let subscriber = tracing_subscriber::fmt()
1103     ///     .map_fmt_fields(|f| f.debug_alt())
1104     ///     .finish();
1105     /// ```
map_fmt_fields<N2>(self, f: impl FnOnce(N) -> N2) -> SubscriberBuilder<N2, E, F, W> where N2: for<'writer> FormatFields<'writer> + 'static,1106     pub fn map_fmt_fields<N2>(self, f: impl FnOnce(N) -> N2) -> SubscriberBuilder<N2, E, F, W>
1107     where
1108         N2: for<'writer> FormatFields<'writer> + 'static,
1109     {
1110         SubscriberBuilder {
1111             filter: self.filter,
1112             inner: self.inner.map_fmt_fields(f),
1113         }
1114     }
1115 
1116     /// Updates the [`MakeWriter`] by applying a function to the existing [`MakeWriter`].
1117     ///
1118     /// This sets the [`MakeWriter`] that the subscriber being built will use to write events.
1119     ///
1120     /// # Examples
1121     ///
1122     /// Redirect output to stderr if level is <= WARN:
1123     ///
1124     /// ```rust
1125     /// use tracing::Level;
1126     /// use tracing_subscriber::fmt::{self, writer::MakeWriterExt};
1127     ///
1128     /// let stderr = std::io::stderr.with_max_level(Level::WARN);
1129     /// let layer = tracing_subscriber::fmt()
1130     ///     .map_writer(move |w| stderr.or_else(w))
1131     ///     .finish();
1132     /// ```
map_writer<W2>(self, f: impl FnOnce(W) -> W2) -> SubscriberBuilder<N, E, F, W2> where W2: for<'writer> MakeWriter<'writer> + 'static,1133     pub fn map_writer<W2>(self, f: impl FnOnce(W) -> W2) -> SubscriberBuilder<N, E, F, W2>
1134     where
1135         W2: for<'writer> MakeWriter<'writer> + 'static,
1136     {
1137         SubscriberBuilder {
1138             filter: self.filter,
1139             inner: self.inner.map_writer(f),
1140         }
1141     }
1142 }
1143 
1144 /// Install a global tracing subscriber that listens for events and
1145 /// filters based on the value of the [`RUST_LOG` environment variable],
1146 /// if one is not already set.
1147 ///
1148 /// If the `tracing-log` feature is enabled, this will also install
1149 /// the [`LogTracer`] to convert `log` records into `tracing` `Event`s.
1150 ///
1151 /// This is shorthand for
1152 ///
1153 /// ```rust
1154 /// # fn doc() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
1155 /// tracing_subscriber::fmt().try_init()
1156 /// # }
1157 /// ```
1158 ///
1159 ///
1160 /// # Errors
1161 ///
1162 /// Returns an Error if the initialization was unsuccessful,
1163 /// likely because a global subscriber was already installed by another
1164 /// call to `try_init`.
1165 ///
1166 /// [`LogTracer`]:
1167 ///     https://docs.rs/tracing-log/0.1.0/tracing_log/struct.LogTracer.html
1168 /// [`RUST_LOG` environment variable]: crate::filter::EnvFilter::DEFAULT_ENV
try_init() -> Result<(), Box<dyn Error + Send + Sync + 'static>>1169 pub fn try_init() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
1170     let builder = Subscriber::builder();
1171 
1172     #[cfg(feature = "env-filter")]
1173     let builder = builder.with_env_filter(crate::EnvFilter::from_default_env());
1174 
1175     // If `env-filter` is disabled, remove the default max level filter from the
1176     // subscriber; it will be added to the `Targets` filter instead if no filter
1177     // is set in `RUST_LOG`.
1178     // Replacing the default `LevelFilter` with an `EnvFilter` would imply this,
1179     // but we can't replace the builder's filter with a `Targets` filter yet.
1180     #[cfg(not(feature = "env-filter"))]
1181     let builder = builder.with_max_level(LevelFilter::TRACE);
1182 
1183     let subscriber = builder.finish();
1184     #[cfg(not(feature = "env-filter"))]
1185     let subscriber = {
1186         use crate::{filter::Targets, layer::SubscriberExt};
1187         use std::{env, str::FromStr};
1188         let targets = match env::var("RUST_LOG") {
1189             Ok(var) => Targets::from_str(&var)
1190                 .map_err(|e| {
1191                     eprintln!("Ignoring `RUST_LOG={:?}`: {}", var, e);
1192                 })
1193                 .unwrap_or_default(),
1194             Err(env::VarError::NotPresent) => {
1195                 Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL)
1196             }
1197             Err(e) => {
1198                 eprintln!("Ignoring `RUST_LOG`: {}", e);
1199                 Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL)
1200             }
1201         };
1202         subscriber.with(targets)
1203     };
1204 
1205     subscriber.try_init().map_err(Into::into)
1206 }
1207 
1208 /// Install a global tracing subscriber that listens for events and
1209 /// filters based on the value of the [`RUST_LOG` environment variable].
1210 ///
1211 /// The configuration of the subscriber initialized by this function
1212 /// depends on what [feature flags](crate#feature-flags) are enabled.
1213 ///
1214 /// If the `tracing-log` feature is enabled, this will also install
1215 /// the LogTracer to convert `Log` records into `tracing` `Event`s.
1216 ///
1217 /// If the `env-filter` feature is enabled, this is shorthand for
1218 ///
1219 /// ```rust
1220 /// # use tracing_subscriber::EnvFilter;
1221 /// tracing_subscriber::fmt()
1222 ///     .with_env_filter(EnvFilter::from_default_env())
1223 ///     .init();
1224 /// ```
1225 ///
1226 /// # Panics
1227 /// Panics if the initialization was unsuccessful, likely because a
1228 /// global subscriber was already installed by another call to `try_init`.
1229 ///
1230 /// [`RUST_LOG` environment variable]: crate::filter::EnvFilter::DEFAULT_ENV
init()1231 pub fn init() {
1232     try_init().expect("Unable to install global subscriber")
1233 }
1234 
1235 #[cfg(test)]
1236 mod test {
1237     use crate::{
1238         filter::LevelFilter,
1239         fmt::{
1240             format::{self, Format},
1241             time,
1242             writer::MakeWriter,
1243             Subscriber,
1244         },
1245     };
1246     use std::{
1247         io,
1248         sync::{Arc, Mutex, MutexGuard, TryLockError},
1249     };
1250     use tracing_core::dispatcher::Dispatch;
1251 
1252     pub(crate) struct MockWriter {
1253         buf: Arc<Mutex<Vec<u8>>>,
1254     }
1255 
1256     impl MockWriter {
new(buf: Arc<Mutex<Vec<u8>>>) -> Self1257         pub(crate) fn new(buf: Arc<Mutex<Vec<u8>>>) -> Self {
1258             Self { buf }
1259         }
1260 
map_error<Guard>(err: TryLockError<Guard>) -> io::Error1261         pub(crate) fn map_error<Guard>(err: TryLockError<Guard>) -> io::Error {
1262             match err {
1263                 TryLockError::WouldBlock => io::Error::from(io::ErrorKind::WouldBlock),
1264                 TryLockError::Poisoned(_) => io::Error::from(io::ErrorKind::Other),
1265             }
1266         }
1267 
buf(&self) -> io::Result<MutexGuard<'_, Vec<u8>>>1268         pub(crate) fn buf(&self) -> io::Result<MutexGuard<'_, Vec<u8>>> {
1269             self.buf.try_lock().map_err(Self::map_error)
1270         }
1271     }
1272 
1273     impl io::Write for MockWriter {
write(&mut self, buf: &[u8]) -> io::Result<usize>1274         fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
1275             self.buf()?.write(buf)
1276         }
1277 
flush(&mut self) -> io::Result<()>1278         fn flush(&mut self) -> io::Result<()> {
1279             self.buf()?.flush()
1280         }
1281     }
1282 
1283     #[derive(Clone, Default)]
1284     pub(crate) struct MockMakeWriter {
1285         buf: Arc<Mutex<Vec<u8>>>,
1286     }
1287 
1288     impl MockMakeWriter {
new(buf: Arc<Mutex<Vec<u8>>>) -> Self1289         pub(crate) fn new(buf: Arc<Mutex<Vec<u8>>>) -> Self {
1290             Self { buf }
1291         }
1292 
1293         #[cfg(feature = "json")]
buf(&self) -> MutexGuard<'_, Vec<u8>>1294         pub(crate) fn buf(&self) -> MutexGuard<'_, Vec<u8>> {
1295             self.buf.lock().unwrap()
1296         }
1297 
get_string(&self) -> String1298         pub(crate) fn get_string(&self) -> String {
1299             let mut buf = self.buf.lock().expect("lock shouldn't be poisoned");
1300             let string = std::str::from_utf8(&buf[..])
1301                 .expect("formatter should not have produced invalid utf-8")
1302                 .to_owned();
1303             buf.clear();
1304             string
1305         }
1306     }
1307 
1308     impl<'a> MakeWriter<'a> for MockMakeWriter {
1309         type Writer = MockWriter;
1310 
make_writer(&'a self) -> Self::Writer1311         fn make_writer(&'a self) -> Self::Writer {
1312             MockWriter::new(self.buf.clone())
1313         }
1314     }
1315 
1316     #[test]
impls()1317     fn impls() {
1318         let f = Format::default().with_timer(time::Uptime::default());
1319         let subscriber = Subscriber::builder().event_format(f).finish();
1320         let _dispatch = Dispatch::new(subscriber);
1321 
1322         let f = format::Format::default();
1323         let subscriber = Subscriber::builder().event_format(f).finish();
1324         let _dispatch = Dispatch::new(subscriber);
1325 
1326         let f = format::Format::default().compact();
1327         let subscriber = Subscriber::builder().event_format(f).finish();
1328         let _dispatch = Dispatch::new(subscriber);
1329     }
1330 
1331     #[test]
subscriber_downcasts()1332     fn subscriber_downcasts() {
1333         let subscriber = Subscriber::builder().finish();
1334         let dispatch = Dispatch::new(subscriber);
1335         assert!(dispatch.downcast_ref::<Subscriber>().is_some());
1336     }
1337 
1338     #[test]
subscriber_downcasts_to_parts()1339     fn subscriber_downcasts_to_parts() {
1340         let subscriber = Subscriber::new();
1341         let dispatch = Dispatch::new(subscriber);
1342         assert!(dispatch.downcast_ref::<format::DefaultFields>().is_some());
1343         assert!(dispatch.downcast_ref::<LevelFilter>().is_some());
1344         assert!(dispatch.downcast_ref::<format::Format>().is_some())
1345     }
1346 
1347     #[test]
is_lookup_span()1348     fn is_lookup_span() {
1349         fn assert_lookup_span<T: for<'a> crate::registry::LookupSpan<'a>>(_: T) {}
1350         let subscriber = Subscriber::new();
1351         assert_lookup_span(subscriber)
1352     }
1353 }
1354