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