/rust/registry/src/index.crates.io-6f17d22bba15001f/tracing-subscriber-0.3.19/src/fmt/mod.rs
Line | Count | Source (jump to first uncovered line) |
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.63+][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"))))] |
321 | 0 | pub fn fmt() -> SubscriberBuilder { |
322 | 0 | SubscriberBuilder::default() |
323 | 0 | } |
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"))))] |
334 | 0 | pub fn layer<S>() -> Layer<S> { |
335 | 0 | Layer::default() |
336 | 0 | } |
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. |
349 | 0 | pub fn builder() -> SubscriberBuilder { |
350 | 0 | SubscriberBuilder::default() |
351 | 0 | } |
352 | | |
353 | | /// Returns a new format subscriber with the default configuration. |
354 | 0 | pub fn new() -> Self { |
355 | 0 | Default::default() |
356 | 0 | } |
357 | | } |
358 | | |
359 | | impl Default for Subscriber { |
360 | 0 | fn default() -> Self { |
361 | 0 | SubscriberBuilder::default().finish() |
362 | 0 | } |
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] |
377 | 0 | fn register_callsite(&self, meta: &'static Metadata<'static>) -> Interest { |
378 | 0 | self.inner.register_callsite(meta) |
379 | 0 | } |
380 | | |
381 | | #[inline] |
382 | 0 | fn enabled(&self, meta: &Metadata<'_>) -> bool { |
383 | 0 | self.inner.enabled(meta) |
384 | 0 | } |
385 | | |
386 | | #[inline] |
387 | 0 | fn new_span(&self, attrs: &span::Attributes<'_>) -> span::Id { |
388 | 0 | self.inner.new_span(attrs) |
389 | 0 | } |
390 | | |
391 | | #[inline] |
392 | 0 | fn record(&self, span: &span::Id, values: &span::Record<'_>) { |
393 | 0 | self.inner.record(span, values) |
394 | 0 | } |
395 | | |
396 | | #[inline] |
397 | 0 | fn record_follows_from(&self, span: &span::Id, follows: &span::Id) { |
398 | 0 | self.inner.record_follows_from(span, follows) |
399 | 0 | } |
400 | | |
401 | | #[inline] |
402 | 0 | fn event_enabled(&self, event: &Event<'_>) -> bool { |
403 | 0 | self.inner.event_enabled(event) |
404 | 0 | } |
405 | | |
406 | | #[inline] |
407 | 0 | fn event(&self, event: &Event<'_>) { |
408 | 0 | self.inner.event(event); |
409 | 0 | } |
410 | | |
411 | | #[inline] |
412 | 0 | fn enter(&self, id: &span::Id) { |
413 | 0 | // TODO: add on_enter hook |
414 | 0 | self.inner.enter(id); |
415 | 0 | } |
416 | | |
417 | | #[inline] |
418 | 0 | fn exit(&self, id: &span::Id) { |
419 | 0 | self.inner.exit(id); |
420 | 0 | } |
421 | | |
422 | | #[inline] |
423 | 0 | fn current_span(&self) -> span::Current { |
424 | 0 | self.inner.current_span() |
425 | 0 | } |
426 | | |
427 | | #[inline] |
428 | 0 | fn clone_span(&self, id: &span::Id) -> span::Id { |
429 | 0 | self.inner.clone_span(id) |
430 | 0 | } |
431 | | |
432 | | #[inline] |
433 | 0 | fn try_close(&self, id: span::Id) -> bool { |
434 | 0 | self.inner.try_close(id) |
435 | 0 | } |
436 | | |
437 | | #[inline] |
438 | 0 | fn max_level_hint(&self) -> Option<tracing_core::LevelFilter> { |
439 | 0 | self.inner.max_level_hint() |
440 | 0 | } |
441 | | |
442 | 0 | unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> { |
443 | 0 | if id == TypeId::of::<Self>() { |
444 | 0 | Some(self as *const Self as *const ()) |
445 | | } else { |
446 | 0 | self.inner.downcast_raw(id) |
447 | | } |
448 | 0 | } |
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 | | |
457 | 0 | fn span_data(&'a self, id: &span::Id) -> Option<Self::Data> { |
458 | 0 | self.inner.span_data(id) |
459 | 0 | } |
460 | | } |
461 | | |
462 | | // ===== impl SubscriberBuilder ===== |
463 | | |
464 | | impl Default for SubscriberBuilder { |
465 | 0 | fn default() -> Self { |
466 | 0 | SubscriberBuilder { |
467 | 0 | filter: Subscriber::DEFAULT_MAX_LEVEL, |
468 | 0 | inner: Default::default(), |
469 | 0 | } |
470 | 0 | .log_internal_errors(true) |
471 | 0 | } |
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`. |
483 | 0 | pub fn finish(self) -> Subscriber<N, E, F, W> { |
484 | 0 | let subscriber = self.inner.with_subscriber(Registry::default()); |
485 | 0 | Subscriber { |
486 | 0 | inner: self.filter.with_subscriber(subscriber), |
487 | 0 | } |
488 | 0 | } |
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`. |
500 | 0 | pub fn try_init(self) -> Result<(), Box<dyn Error + Send + Sync + 'static>> { |
501 | | use crate::util::SubscriberInitExt; |
502 | 0 | self.finish().try_init()?; |
503 | | |
504 | 0 | Ok(()) |
505 | 0 | } |
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`. |
515 | 0 | pub fn init(self) { |
516 | 0 | self.try_init() |
517 | 0 | .expect("Unable to install global subscriber") |
518 | 0 | } |
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 | | { |
529 | 0 | fn from(builder: SubscriberBuilder<N, E, F, W>) -> tracing_core::Dispatch { |
530 | 0 | tracing_core::Dispatch::new(builder.finish()) |
531 | 0 | } |
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 |
552 | 0 | pub fn with_timer<T2>(self, timer: T2) -> SubscriberBuilder<N, format::Format<L, T2>, F, W> { |
553 | 0 | SubscriberBuilder { |
554 | 0 | filter: self.filter, |
555 | 0 | inner: self.inner.with_timer(timer), |
556 | 0 | } |
557 | 0 | } |
558 | | |
559 | | /// Do not emit timestamps with log messages. |
560 | 0 | pub fn without_time(self) -> SubscriberBuilder<N, format::Format<L, ()>, F, W> { |
561 | 0 | SubscriberBuilder { |
562 | 0 | filter: self.filter, |
563 | 0 | inner: self.inner.without_time(), |
564 | 0 | } |
565 | 0 | } |
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() |
608 | 0 | pub fn with_span_events(self, kind: format::FmtSpan) -> Self { |
609 | 0 | SubscriberBuilder { |
610 | 0 | inner: self.inner.with_span_events(kind), |
611 | 0 | ..self |
612 | 0 | } |
613 | 0 | } |
614 | | |
615 | | /// Sets whether or not the formatter emits ANSI terminal escape codes |
616 | | /// for colors and other text formatting. |
617 | | /// |
618 | | /// Enabling ANSI escapes (calling `with_ansi(true)`) requires the "ansi" |
619 | | /// crate feature flag. Calling `with_ansi(true)` without the "ansi" |
620 | | /// feature flag enabled will panic if debug assertions are enabled, or |
621 | | /// print a warning otherwise. |
622 | | /// |
623 | | /// This method itself is still available without the feature flag. This |
624 | | /// is to allow ANSI escape codes to be explicitly *disabled* without |
625 | | /// having to opt-in to the dependencies required to emit ANSI formatting. |
626 | | /// This way, code which constructs a formatter that should never emit |
627 | | /// ANSI escape codes can ensure that they are not used, regardless of |
628 | | /// whether or not other crates in the dependency graph enable the "ansi" |
629 | | /// feature flag. |
630 | 0 | pub fn with_ansi(self, ansi: bool) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { |
631 | 0 | SubscriberBuilder { |
632 | 0 | inner: self.inner.with_ansi(ansi), |
633 | 0 | ..self |
634 | 0 | } |
635 | 0 | } |
636 | | |
637 | | /// Sets whether to write errors from [`FormatEvent`] to the writer. |
638 | | /// Defaults to true. |
639 | | /// |
640 | | /// By default, `fmt::Layer` will write any `FormatEvent`-internal errors to |
641 | | /// the writer. These errors are unlikely and will only occur if there is a |
642 | | /// bug in the `FormatEvent` implementation or its dependencies. |
643 | | /// |
644 | | /// If writing to the writer fails, the error message is printed to stderr |
645 | | /// as a fallback. |
646 | | /// |
647 | | /// [`FormatEvent`]: crate::fmt::FormatEvent |
648 | 0 | pub fn log_internal_errors( |
649 | 0 | self, |
650 | 0 | log_internal_errors: bool, |
651 | 0 | ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { |
652 | 0 | SubscriberBuilder { |
653 | 0 | inner: self.inner.log_internal_errors(log_internal_errors), |
654 | 0 | ..self |
655 | 0 | } |
656 | 0 | } |
657 | | |
658 | | /// Sets whether or not an event's target is displayed. |
659 | 0 | pub fn with_target( |
660 | 0 | self, |
661 | 0 | display_target: bool, |
662 | 0 | ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { |
663 | 0 | SubscriberBuilder { |
664 | 0 | inner: self.inner.with_target(display_target), |
665 | 0 | ..self |
666 | 0 | } |
667 | 0 | } |
668 | | |
669 | | /// Sets whether or not an event's [source code file path][file] is |
670 | | /// displayed. |
671 | | /// |
672 | | /// [file]: tracing_core::Metadata::file |
673 | 0 | pub fn with_file( |
674 | 0 | self, |
675 | 0 | display_filename: bool, |
676 | 0 | ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { |
677 | 0 | SubscriberBuilder { |
678 | 0 | inner: self.inner.with_file(display_filename), |
679 | 0 | ..self |
680 | 0 | } |
681 | 0 | } |
682 | | |
683 | | /// Sets whether or not an event's [source code line number][line] is |
684 | | /// displayed. |
685 | | /// |
686 | | /// [line]: tracing_core::Metadata::line |
687 | 0 | pub fn with_line_number( |
688 | 0 | self, |
689 | 0 | display_line_number: bool, |
690 | 0 | ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { |
691 | 0 | SubscriberBuilder { |
692 | 0 | inner: self.inner.with_line_number(display_line_number), |
693 | 0 | ..self |
694 | 0 | } |
695 | 0 | } |
696 | | |
697 | | /// Sets whether or not an event's level is displayed. |
698 | 0 | pub fn with_level( |
699 | 0 | self, |
700 | 0 | display_level: bool, |
701 | 0 | ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { |
702 | 0 | SubscriberBuilder { |
703 | 0 | inner: self.inner.with_level(display_level), |
704 | 0 | ..self |
705 | 0 | } |
706 | 0 | } |
707 | | |
708 | | /// Sets whether or not the [name] of the current thread is displayed |
709 | | /// when formatting events. |
710 | | /// |
711 | | /// [name]: std::thread#naming-threads |
712 | 0 | pub fn with_thread_names( |
713 | 0 | self, |
714 | 0 | display_thread_names: bool, |
715 | 0 | ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { |
716 | 0 | SubscriberBuilder { |
717 | 0 | inner: self.inner.with_thread_names(display_thread_names), |
718 | 0 | ..self |
719 | 0 | } |
720 | 0 | } |
721 | | |
722 | | /// Sets whether or not the [thread ID] of the current thread is displayed |
723 | | /// when formatting events. |
724 | | /// |
725 | | /// [thread ID]: std::thread::ThreadId |
726 | 0 | pub fn with_thread_ids( |
727 | 0 | self, |
728 | 0 | display_thread_ids: bool, |
729 | 0 | ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { |
730 | 0 | SubscriberBuilder { |
731 | 0 | inner: self.inner.with_thread_ids(display_thread_ids), |
732 | 0 | ..self |
733 | 0 | } |
734 | 0 | } |
735 | | |
736 | | /// Sets the subscriber being built to use a less verbose formatter. |
737 | | /// |
738 | | /// See [`format::Compact`]. |
739 | 0 | pub fn compact(self) -> SubscriberBuilder<N, format::Format<format::Compact, T>, F, W> |
740 | 0 | where |
741 | 0 | N: for<'writer> FormatFields<'writer> + 'static, |
742 | 0 | { |
743 | 0 | SubscriberBuilder { |
744 | 0 | filter: self.filter, |
745 | 0 | inner: self.inner.compact(), |
746 | 0 | } |
747 | 0 | } |
748 | | |
749 | | /// Sets the subscriber being built to use an [excessively pretty, human-readable formatter](crate::fmt::format::Pretty). |
750 | | #[cfg(feature = "ansi")] |
751 | | #[cfg_attr(docsrs, doc(cfg(feature = "ansi")))] |
752 | | pub fn pretty( |
753 | | self, |
754 | | ) -> SubscriberBuilder<format::Pretty, format::Format<format::Pretty, T>, F, W> { |
755 | | SubscriberBuilder { |
756 | | filter: self.filter, |
757 | | inner: self.inner.pretty(), |
758 | | } |
759 | | } |
760 | | |
761 | | /// Sets the subscriber being built to use a JSON formatter. |
762 | | /// |
763 | | /// See [`format::Json`] for details. |
764 | | #[cfg(feature = "json")] |
765 | | #[cfg_attr(docsrs, doc(cfg(feature = "json")))] |
766 | | pub fn json( |
767 | | self, |
768 | | ) -> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> |
769 | | where |
770 | | N: for<'writer> FormatFields<'writer> + 'static, |
771 | | { |
772 | | SubscriberBuilder { |
773 | | filter: self.filter, |
774 | | inner: self.inner.json(), |
775 | | } |
776 | | } |
777 | | } |
778 | | |
779 | | #[cfg(feature = "json")] |
780 | | #[cfg_attr(docsrs, doc(cfg(feature = "json")))] |
781 | | impl<T, F, W> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> { |
782 | | /// Sets the json subscriber being built to flatten event metadata. |
783 | | /// |
784 | | /// See [`format::Json`] for details. |
785 | | pub fn flatten_event( |
786 | | self, |
787 | | flatten_event: bool, |
788 | | ) -> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> { |
789 | | SubscriberBuilder { |
790 | | filter: self.filter, |
791 | | inner: self.inner.flatten_event(flatten_event), |
792 | | } |
793 | | } |
794 | | |
795 | | /// Sets whether or not the JSON subscriber being built will include the current span |
796 | | /// in formatted events. |
797 | | /// |
798 | | /// See [`format::Json`] for details. |
799 | | pub fn with_current_span( |
800 | | self, |
801 | | display_current_span: bool, |
802 | | ) -> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> { |
803 | | SubscriberBuilder { |
804 | | filter: self.filter, |
805 | | inner: self.inner.with_current_span(display_current_span), |
806 | | } |
807 | | } |
808 | | |
809 | | /// Sets whether or not the JSON subscriber being built will include a list (from |
810 | | /// root to leaf) of all currently entered spans in formatted events. |
811 | | /// |
812 | | /// See [`format::Json`] for details. |
813 | | pub fn with_span_list( |
814 | | self, |
815 | | display_span_list: bool, |
816 | | ) -> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> { |
817 | | SubscriberBuilder { |
818 | | filter: self.filter, |
819 | | inner: self.inner.with_span_list(display_span_list), |
820 | | } |
821 | | } |
822 | | } |
823 | | |
824 | | #[cfg(feature = "env-filter")] |
825 | | #[cfg_attr(docsrs, doc(cfg(feature = "env-filter")))] |
826 | | impl<N, E, W> SubscriberBuilder<N, E, crate::EnvFilter, W> |
827 | | where |
828 | | Formatter<N, E, W>: tracing_core::Subscriber + 'static, |
829 | | { |
830 | | /// Configures the subscriber being built to allow filter reloading at |
831 | | /// runtime. |
832 | | pub fn with_filter_reloading( |
833 | | self, |
834 | | ) -> SubscriberBuilder<N, E, crate::reload::Layer<crate::EnvFilter, Formatter<N, E, W>>, W> |
835 | | { |
836 | | let (filter, _) = crate::reload::Layer::new(self.filter); |
837 | | SubscriberBuilder { |
838 | | filter, |
839 | | inner: self.inner, |
840 | | } |
841 | | } |
842 | | } |
843 | | |
844 | | #[cfg(feature = "env-filter")] |
845 | | #[cfg_attr(docsrs, doc(cfg(feature = "env-filter")))] |
846 | | impl<N, E, W> SubscriberBuilder<N, E, crate::reload::Layer<crate::EnvFilter, Formatter<N, E, W>>, W> |
847 | | where |
848 | | Formatter<N, E, W>: tracing_core::Subscriber + 'static, |
849 | | { |
850 | | /// Returns a `Handle` that may be used to reload the constructed subscriber's |
851 | | /// filter. |
852 | | pub fn reload_handle(&self) -> crate::reload::Handle<crate::EnvFilter, Formatter<N, E, W>> { |
853 | | self.filter.handle() |
854 | | } |
855 | | } |
856 | | |
857 | | impl<N, E, F, W> SubscriberBuilder<N, E, F, W> { |
858 | | /// Sets the field formatter that the subscriber being built will use to record |
859 | | /// fields. |
860 | | /// |
861 | | /// For example: |
862 | | /// ```rust |
863 | | /// use tracing_subscriber::fmt::format; |
864 | | /// use tracing_subscriber::prelude::*; |
865 | | /// |
866 | | /// let formatter = |
867 | | /// // Construct a custom formatter for `Debug` fields |
868 | | /// format::debug_fn(|writer, field, value| write!(writer, "{}: {:?}", field, value)) |
869 | | /// // Use the `tracing_subscriber::MakeFmtExt` trait to wrap the |
870 | | /// // formatter so that a delimiter is added between fields. |
871 | | /// .delimited(", "); |
872 | | /// |
873 | | /// let subscriber = tracing_subscriber::fmt() |
874 | | /// .fmt_fields(formatter) |
875 | | /// .finish(); |
876 | | /// # drop(subscriber) |
877 | | /// ``` |
878 | 0 | pub fn fmt_fields<N2>(self, fmt_fields: N2) -> SubscriberBuilder<N2, E, F, W> |
879 | 0 | where |
880 | 0 | N2: for<'writer> FormatFields<'writer> + 'static, |
881 | 0 | { |
882 | 0 | SubscriberBuilder { |
883 | 0 | filter: self.filter, |
884 | 0 | inner: self.inner.fmt_fields(fmt_fields), |
885 | 0 | } |
886 | 0 | } |
887 | | |
888 | | /// Sets the [`EnvFilter`] that the subscriber will use to determine if |
889 | | /// a span or event is enabled. |
890 | | /// |
891 | | /// Note that this method requires the "env-filter" feature flag to be enabled. |
892 | | /// |
893 | | /// If a filter was previously set, or a maximum level was set by the |
894 | | /// [`with_max_level`] method, that value is replaced by the new filter. |
895 | | /// |
896 | | /// # Examples |
897 | | /// |
898 | | /// Setting a filter based on the value of the `RUST_LOG` environment |
899 | | /// variable: |
900 | | /// ```rust |
901 | | /// use tracing_subscriber::{fmt, EnvFilter}; |
902 | | /// |
903 | | /// fmt() |
904 | | /// .with_env_filter(EnvFilter::from_default_env()) |
905 | | /// .init(); |
906 | | /// ``` |
907 | | /// |
908 | | /// Setting a filter based on a pre-set filter directive string: |
909 | | /// ```rust |
910 | | /// use tracing_subscriber::fmt; |
911 | | /// |
912 | | /// fmt() |
913 | | /// .with_env_filter("my_crate=info,my_crate::my_mod=debug,[my_span]=trace") |
914 | | /// .init(); |
915 | | /// ``` |
916 | | /// |
917 | | /// Adding additional directives to a filter constructed from an env var: |
918 | | /// ```rust |
919 | | /// use tracing_subscriber::{fmt, filter::{EnvFilter, LevelFilter}}; |
920 | | /// |
921 | | /// # fn filter() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { |
922 | | /// let filter = EnvFilter::try_from_env("MY_CUSTOM_FILTER_ENV_VAR")? |
923 | | /// // Set the base level when not matched by other directives to WARN. |
924 | | /// .add_directive(LevelFilter::WARN.into()) |
925 | | /// // Set the max level for `my_crate::my_mod` to DEBUG, overriding |
926 | | /// // any directives parsed from the env variable. |
927 | | /// .add_directive("my_crate::my_mod=debug".parse()?); |
928 | | /// |
929 | | /// fmt() |
930 | | /// .with_env_filter(filter) |
931 | | /// .try_init()?; |
932 | | /// # Ok(())} |
933 | | /// ``` |
934 | | /// [`EnvFilter`]: super::filter::EnvFilter |
935 | | /// [`with_max_level`]: SubscriberBuilder::with_max_level() |
936 | | #[cfg(feature = "env-filter")] |
937 | | #[cfg_attr(docsrs, doc(cfg(feature = "env-filter")))] |
938 | | pub fn with_env_filter( |
939 | | self, |
940 | | filter: impl Into<crate::EnvFilter>, |
941 | | ) -> SubscriberBuilder<N, E, crate::EnvFilter, W> |
942 | | where |
943 | | Formatter<N, E, W>: tracing_core::Subscriber + 'static, |
944 | | { |
945 | | let filter = filter.into(); |
946 | | SubscriberBuilder { |
947 | | filter, |
948 | | inner: self.inner, |
949 | | } |
950 | | } |
951 | | |
952 | | /// Sets the maximum [verbosity level] that will be enabled by the |
953 | | /// subscriber. |
954 | | /// |
955 | | /// If the max level has already been set, or a [`EnvFilter`] was added by |
956 | | /// [`with_env_filter`], this replaces that configuration with the new |
957 | | /// maximum level. |
958 | | /// |
959 | | /// # Examples |
960 | | /// |
961 | | /// Enable up to the `DEBUG` verbosity level: |
962 | | /// ```rust |
963 | | /// use tracing_subscriber::fmt; |
964 | | /// use tracing::Level; |
965 | | /// |
966 | | /// fmt() |
967 | | /// .with_max_level(Level::DEBUG) |
968 | | /// .init(); |
969 | | /// ``` |
970 | | /// This subscriber won't record any spans or events! |
971 | | /// ```rust |
972 | | /// use tracing_subscriber::{fmt, filter::LevelFilter}; |
973 | | /// |
974 | | /// let subscriber = fmt() |
975 | | /// .with_max_level(LevelFilter::OFF) |
976 | | /// .finish(); |
977 | | /// ``` |
978 | | /// [verbosity level]: tracing_core::Level |
979 | | /// [`EnvFilter`]: struct@crate::filter::EnvFilter |
980 | | /// [`with_env_filter`]: fn@Self::with_env_filter |
981 | 0 | pub fn with_max_level( |
982 | 0 | self, |
983 | 0 | filter: impl Into<LevelFilter>, |
984 | 0 | ) -> SubscriberBuilder<N, E, LevelFilter, W> { |
985 | 0 | let filter = filter.into(); |
986 | 0 | SubscriberBuilder { |
987 | 0 | filter, |
988 | 0 | inner: self.inner, |
989 | 0 | } |
990 | 0 | } |
991 | | |
992 | | /// Sets the [event formatter][`FormatEvent`] that the subscriber being built |
993 | | /// will use to format events that occur. |
994 | | /// |
995 | | /// The event formatter may be any type implementing the [`FormatEvent`] |
996 | | /// trait, which is implemented for all functions taking a [`FmtContext`], a |
997 | | /// [`Writer`], and an [`Event`]. |
998 | | /// |
999 | | /// # Examples |
1000 | | /// |
1001 | | /// Setting a type implementing [`FormatEvent`] as the formatter: |
1002 | | /// |
1003 | | /// ```rust |
1004 | | /// use tracing_subscriber::fmt::format; |
1005 | | /// |
1006 | | /// let subscriber = tracing_subscriber::fmt() |
1007 | | /// .event_format(format().compact()) |
1008 | | /// .finish(); |
1009 | | /// ``` |
1010 | | /// |
1011 | | /// [`Writer`]: struct@self::format::Writer |
1012 | 0 | pub fn event_format<E2>(self, fmt_event: E2) -> SubscriberBuilder<N, E2, F, W> |
1013 | 0 | where |
1014 | 0 | E2: FormatEvent<Registry, N> + 'static, |
1015 | 0 | N: for<'writer> FormatFields<'writer> + 'static, |
1016 | 0 | W: for<'writer> MakeWriter<'writer> + 'static, |
1017 | 0 | { |
1018 | 0 | SubscriberBuilder { |
1019 | 0 | filter: self.filter, |
1020 | 0 | inner: self.inner.event_format(fmt_event), |
1021 | 0 | } |
1022 | 0 | } |
1023 | | |
1024 | | /// Sets the [`MakeWriter`] that the subscriber being built will use to write events. |
1025 | | /// |
1026 | | /// # Examples |
1027 | | /// |
1028 | | /// Using `stderr` rather than `stdout`: |
1029 | | /// |
1030 | | /// ```rust |
1031 | | /// use tracing_subscriber::fmt; |
1032 | | /// use std::io; |
1033 | | /// |
1034 | | /// fmt() |
1035 | | /// .with_writer(io::stderr) |
1036 | | /// .init(); |
1037 | | /// ``` |
1038 | 0 | pub fn with_writer<W2>(self, make_writer: W2) -> SubscriberBuilder<N, E, F, W2> |
1039 | 0 | where |
1040 | 0 | W2: for<'writer> MakeWriter<'writer> + 'static, |
1041 | 0 | { |
1042 | 0 | SubscriberBuilder { |
1043 | 0 | filter: self.filter, |
1044 | 0 | inner: self.inner.with_writer(make_writer), |
1045 | 0 | } |
1046 | 0 | } |
1047 | | |
1048 | | /// Configures the subscriber to support [`libtest`'s output capturing][capturing] when used in |
1049 | | /// unit tests. |
1050 | | /// |
1051 | | /// See [`TestWriter`] for additional details. |
1052 | | /// |
1053 | | /// # Examples |
1054 | | /// |
1055 | | /// Using [`TestWriter`] to let `cargo test` capture test output. Note that we do not install it |
1056 | | /// globally as it may cause conflicts. |
1057 | | /// |
1058 | | /// ```rust |
1059 | | /// use tracing_subscriber::fmt; |
1060 | | /// use tracing::subscriber; |
1061 | | /// |
1062 | | /// subscriber::set_default( |
1063 | | /// fmt() |
1064 | | /// .with_test_writer() |
1065 | | /// .finish() |
1066 | | /// ); |
1067 | | /// ``` |
1068 | | /// |
1069 | | /// [capturing]: |
1070 | | /// https://doc.rust-lang.org/book/ch11-02-running-tests.html#showing-function-output |
1071 | | /// [`TestWriter`]: writer::TestWriter |
1072 | 0 | pub fn with_test_writer(self) -> SubscriberBuilder<N, E, F, TestWriter> { |
1073 | 0 | SubscriberBuilder { |
1074 | 0 | filter: self.filter, |
1075 | 0 | inner: self.inner.with_writer(TestWriter::default()), |
1076 | 0 | } |
1077 | 0 | } |
1078 | | |
1079 | | /// Updates the event formatter by applying a function to the existing event formatter. |
1080 | | /// |
1081 | | /// This sets the event formatter that the subscriber being built will use to record fields. |
1082 | | /// |
1083 | | /// # Examples |
1084 | | /// |
1085 | | /// Updating an event formatter: |
1086 | | /// |
1087 | | /// ```rust |
1088 | | /// let subscriber = tracing_subscriber::fmt() |
1089 | | /// .map_event_format(|e| e.compact()) |
1090 | | /// .finish(); |
1091 | | /// ``` |
1092 | 0 | pub fn map_event_format<E2>(self, f: impl FnOnce(E) -> E2) -> SubscriberBuilder<N, E2, F, W> |
1093 | 0 | where |
1094 | 0 | E2: FormatEvent<Registry, N> + 'static, |
1095 | 0 | N: for<'writer> FormatFields<'writer> + 'static, |
1096 | 0 | W: for<'writer> MakeWriter<'writer> + 'static, |
1097 | 0 | { |
1098 | 0 | SubscriberBuilder { |
1099 | 0 | filter: self.filter, |
1100 | 0 | inner: self.inner.map_event_format(f), |
1101 | 0 | } |
1102 | 0 | } |
1103 | | |
1104 | | /// Updates the field formatter by applying a function to the existing field formatter. |
1105 | | /// |
1106 | | /// This sets the field formatter that the subscriber being built will use to record fields. |
1107 | | /// |
1108 | | /// # Examples |
1109 | | /// |
1110 | | /// Updating a field formatter: |
1111 | | /// |
1112 | | /// ```rust |
1113 | | /// use tracing_subscriber::field::MakeExt; |
1114 | | /// let subscriber = tracing_subscriber::fmt() |
1115 | | /// .map_fmt_fields(|f| f.debug_alt()) |
1116 | | /// .finish(); |
1117 | | /// ``` |
1118 | 0 | pub fn map_fmt_fields<N2>(self, f: impl FnOnce(N) -> N2) -> SubscriberBuilder<N2, E, F, W> |
1119 | 0 | where |
1120 | 0 | N2: for<'writer> FormatFields<'writer> + 'static, |
1121 | 0 | { |
1122 | 0 | SubscriberBuilder { |
1123 | 0 | filter: self.filter, |
1124 | 0 | inner: self.inner.map_fmt_fields(f), |
1125 | 0 | } |
1126 | 0 | } |
1127 | | |
1128 | | /// Updates the [`MakeWriter`] by applying a function to the existing [`MakeWriter`]. |
1129 | | /// |
1130 | | /// This sets the [`MakeWriter`] that the subscriber being built will use to write events. |
1131 | | /// |
1132 | | /// # Examples |
1133 | | /// |
1134 | | /// Redirect output to stderr if level is <= WARN: |
1135 | | /// |
1136 | | /// ```rust |
1137 | | /// use tracing::Level; |
1138 | | /// use tracing_subscriber::fmt::{self, writer::MakeWriterExt}; |
1139 | | /// |
1140 | | /// let stderr = std::io::stderr.with_max_level(Level::WARN); |
1141 | | /// let layer = tracing_subscriber::fmt() |
1142 | | /// .map_writer(move |w| stderr.or_else(w)) |
1143 | | /// .finish(); |
1144 | | /// ``` |
1145 | 0 | pub fn map_writer<W2>(self, f: impl FnOnce(W) -> W2) -> SubscriberBuilder<N, E, F, W2> |
1146 | 0 | where |
1147 | 0 | W2: for<'writer> MakeWriter<'writer> + 'static, |
1148 | 0 | { |
1149 | 0 | SubscriberBuilder { |
1150 | 0 | filter: self.filter, |
1151 | 0 | inner: self.inner.map_writer(f), |
1152 | 0 | } |
1153 | 0 | } |
1154 | | } |
1155 | | |
1156 | | /// Install a global tracing subscriber that listens for events and |
1157 | | /// filters based on the value of the [`RUST_LOG` environment variable], |
1158 | | /// if one is not already set. |
1159 | | /// |
1160 | | /// If the `tracing-log` feature is enabled, this will also install |
1161 | | /// the [`LogTracer`] to convert `log` records into `tracing` `Event`s. |
1162 | | /// |
1163 | | /// This is shorthand for |
1164 | | /// |
1165 | | /// ```rust |
1166 | | /// # fn doc() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { |
1167 | | /// tracing_subscriber::fmt().try_init() |
1168 | | /// # } |
1169 | | /// ``` |
1170 | | /// |
1171 | | /// |
1172 | | /// # Errors |
1173 | | /// |
1174 | | /// Returns an Error if the initialization was unsuccessful, |
1175 | | /// likely because a global subscriber was already installed by another |
1176 | | /// call to `try_init`. |
1177 | | /// |
1178 | | /// [`LogTracer`]: |
1179 | | /// https://docs.rs/tracing-log/0.1.0/tracing_log/struct.LogTracer.html |
1180 | | /// [`RUST_LOG` environment variable]: crate::filter::EnvFilter::DEFAULT_ENV |
1181 | 0 | pub fn try_init() -> Result<(), Box<dyn Error + Send + Sync + 'static>> { |
1182 | 0 | let builder = Subscriber::builder(); |
1183 | 0 |
|
1184 | 0 | #[cfg(feature = "env-filter")] |
1185 | 0 | let builder = builder.with_env_filter(crate::EnvFilter::from_default_env()); |
1186 | 0 |
|
1187 | 0 | // If `env-filter` is disabled, remove the default max level filter from the |
1188 | 0 | // subscriber; it will be added to the `Targets` filter instead if no filter |
1189 | 0 | // is set in `RUST_LOG`. |
1190 | 0 | // Replacing the default `LevelFilter` with an `EnvFilter` would imply this, |
1191 | 0 | // but we can't replace the builder's filter with a `Targets` filter yet. |
1192 | 0 | #[cfg(not(feature = "env-filter"))] |
1193 | 0 | let builder = builder.with_max_level(LevelFilter::TRACE); |
1194 | 0 |
|
1195 | 0 | let subscriber = builder.finish(); |
1196 | | #[cfg(not(feature = "env-filter"))] |
1197 | 0 | let subscriber = { |
1198 | | use crate::{filter::Targets, layer::SubscriberExt}; |
1199 | | use std::{env, str::FromStr}; |
1200 | 0 | let targets = match env::var("RUST_LOG") { |
1201 | 0 | Ok(var) => Targets::from_str(&var) |
1202 | 0 | .map_err(|e| { |
1203 | 0 | eprintln!("Ignoring `RUST_LOG={:?}`: {}", var, e); |
1204 | 0 | }) |
1205 | 0 | .unwrap_or_default(), |
1206 | | Err(env::VarError::NotPresent) => { |
1207 | 0 | Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL) |
1208 | | } |
1209 | 0 | Err(e) => { |
1210 | 0 | eprintln!("Ignoring `RUST_LOG`: {}", e); |
1211 | 0 | Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL) |
1212 | | } |
1213 | | }; |
1214 | 0 | subscriber.with(targets) |
1215 | 0 | }; |
1216 | 0 |
|
1217 | 0 | subscriber.try_init().map_err(Into::into) |
1218 | 0 | } |
1219 | | |
1220 | | /// Install a global tracing subscriber that listens for events and |
1221 | | /// filters based on the value of the [`RUST_LOG` environment variable]. |
1222 | | /// |
1223 | | /// The configuration of the subscriber initialized by this function |
1224 | | /// depends on what [feature flags](crate#feature-flags) are enabled. |
1225 | | /// |
1226 | | /// If the `tracing-log` feature is enabled, this will also install |
1227 | | /// the LogTracer to convert `Log` records into `tracing` `Event`s. |
1228 | | /// |
1229 | | /// If the `env-filter` feature is enabled, this is shorthand for |
1230 | | /// |
1231 | | /// ```rust |
1232 | | /// # use tracing_subscriber::EnvFilter; |
1233 | | /// tracing_subscriber::fmt() |
1234 | | /// .with_env_filter(EnvFilter::from_default_env()) |
1235 | | /// .init(); |
1236 | | /// ``` |
1237 | | /// |
1238 | | /// # Panics |
1239 | | /// Panics if the initialization was unsuccessful, likely because a |
1240 | | /// global subscriber was already installed by another call to `try_init`. |
1241 | | /// |
1242 | | /// [`RUST_LOG` environment variable]: crate::filter::EnvFilter::DEFAULT_ENV |
1243 | 0 | pub fn init() { |
1244 | 0 | try_init().expect("Unable to install global subscriber") |
1245 | 0 | } |
1246 | | |
1247 | | #[cfg(test)] |
1248 | | mod test { |
1249 | | use crate::{ |
1250 | | filter::LevelFilter, |
1251 | | fmt::{ |
1252 | | format::{self, Format}, |
1253 | | time, |
1254 | | writer::MakeWriter, |
1255 | | Subscriber, |
1256 | | }, |
1257 | | }; |
1258 | | use std::{ |
1259 | | io, |
1260 | | sync::{Arc, Mutex, MutexGuard, TryLockError}, |
1261 | | }; |
1262 | | use tracing_core::dispatcher::Dispatch; |
1263 | | |
1264 | | pub(crate) struct MockWriter { |
1265 | | buf: Arc<Mutex<Vec<u8>>>, |
1266 | | } |
1267 | | |
1268 | | impl MockWriter { |
1269 | | pub(crate) fn new(buf: Arc<Mutex<Vec<u8>>>) -> Self { |
1270 | | Self { buf } |
1271 | | } |
1272 | | |
1273 | | pub(crate) fn map_error<Guard>(err: TryLockError<Guard>) -> io::Error { |
1274 | | match err { |
1275 | | TryLockError::WouldBlock => io::Error::from(io::ErrorKind::WouldBlock), |
1276 | | TryLockError::Poisoned(_) => io::Error::from(io::ErrorKind::Other), |
1277 | | } |
1278 | | } |
1279 | | |
1280 | | pub(crate) fn buf(&self) -> io::Result<MutexGuard<'_, Vec<u8>>> { |
1281 | | self.buf.try_lock().map_err(Self::map_error) |
1282 | | } |
1283 | | } |
1284 | | |
1285 | | impl io::Write for MockWriter { |
1286 | | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
1287 | | self.buf()?.write(buf) |
1288 | | } |
1289 | | |
1290 | | fn flush(&mut self) -> io::Result<()> { |
1291 | | self.buf()?.flush() |
1292 | | } |
1293 | | } |
1294 | | |
1295 | | #[derive(Clone, Default)] |
1296 | | pub(crate) struct MockMakeWriter { |
1297 | | buf: Arc<Mutex<Vec<u8>>>, |
1298 | | } |
1299 | | |
1300 | | impl MockMakeWriter { |
1301 | | pub(crate) fn new(buf: Arc<Mutex<Vec<u8>>>) -> Self { |
1302 | | Self { buf } |
1303 | | } |
1304 | | |
1305 | | // this is currently only used by the JSON formatter tests. if we need |
1306 | | // it elsewhere in the future, feel free to remove the `#[cfg]` |
1307 | | // attribute! |
1308 | | #[cfg(feature = "json")] |
1309 | | pub(crate) fn buf(&self) -> MutexGuard<'_, Vec<u8>> { |
1310 | | self.buf.lock().unwrap() |
1311 | | } |
1312 | | |
1313 | | pub(crate) fn get_string(&self) -> String { |
1314 | | let mut buf = self.buf.lock().expect("lock shouldn't be poisoned"); |
1315 | | let string = std::str::from_utf8(&buf[..]) |
1316 | | .expect("formatter should not have produced invalid utf-8") |
1317 | | .to_owned(); |
1318 | | buf.clear(); |
1319 | | string |
1320 | | } |
1321 | | } |
1322 | | |
1323 | | impl<'a> MakeWriter<'a> for MockMakeWriter { |
1324 | | type Writer = MockWriter; |
1325 | | |
1326 | | fn make_writer(&'a self) -> Self::Writer { |
1327 | | MockWriter::new(self.buf.clone()) |
1328 | | } |
1329 | | } |
1330 | | |
1331 | | #[test] |
1332 | | fn impls() { |
1333 | | let f = Format::default().with_timer(time::Uptime::default()); |
1334 | | let subscriber = Subscriber::builder().event_format(f).finish(); |
1335 | | let _dispatch = Dispatch::new(subscriber); |
1336 | | |
1337 | | let f = format::Format::default(); |
1338 | | let subscriber = Subscriber::builder().event_format(f).finish(); |
1339 | | let _dispatch = Dispatch::new(subscriber); |
1340 | | |
1341 | | let f = format::Format::default().compact(); |
1342 | | let subscriber = Subscriber::builder().event_format(f).finish(); |
1343 | | let _dispatch = Dispatch::new(subscriber); |
1344 | | } |
1345 | | |
1346 | | #[test] |
1347 | | fn subscriber_downcasts() { |
1348 | | let subscriber = Subscriber::builder().finish(); |
1349 | | let dispatch = Dispatch::new(subscriber); |
1350 | | assert!(dispatch.downcast_ref::<Subscriber>().is_some()); |
1351 | | } |
1352 | | |
1353 | | #[test] |
1354 | | fn subscriber_downcasts_to_parts() { |
1355 | | let subscriber = Subscriber::new(); |
1356 | | let dispatch = Dispatch::new(subscriber); |
1357 | | assert!(dispatch.downcast_ref::<format::DefaultFields>().is_some()); |
1358 | | assert!(dispatch.downcast_ref::<LevelFilter>().is_some()); |
1359 | | assert!(dispatch.downcast_ref::<format::Format>().is_some()) |
1360 | | } |
1361 | | |
1362 | | #[test] |
1363 | | fn is_lookup_span() { |
1364 | | fn assert_lookup_span<T: for<'a> crate::registry::LookupSpan<'a>>(_: T) {} |
1365 | | let subscriber = Subscriber::new(); |
1366 | | assert_lookup_span(subscriber) |
1367 | | } |
1368 | | } |