oxedyne/fe2o3/fe2o3_text/tests/annealer_corpus/tracing_sub.rs
44.6 KiB, 1 run
created by r1870400018:11868, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! A Collector 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 [`Collect`] 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.65+][msrv]* |
| 20 | //! |
| 21 | //! [msrv]: ../index.html#supported-rust-versions |
| 22 | //! |
| 23 | //! Add the following to your executable to initialize the default collector: |
| 24 | //! ```rust |
| 25 | //! use tracing_subscriber; |
| 26 | //! |
| 27 | //! tracing_subscriber::fmt::init(); |
| 28 | //! ``` |
| 29 | //! |
| 30 | //! ## Filtering Events with Environment Variables |
| 31 | //! |
| 32 | //! The default collector 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 collector instead of using the defaults with |
| 49 | //! the following functions: |
| 50 | //! |
| 51 | //! ## Collector |
| 52 | //! |
| 53 | //! The [`FmtCollector`] formats and records `tracing` events as line-oriented logs. |
| 54 | //! You can create one by calling: |
| 55 | //! |
| 56 | //! ```rust |
| 57 | //! let collector = tracing_subscriber::fmt() |
| 58 | //! // ... add configuration |
| 59 | //! .finish(); |
| 60 | //! ``` |
| 61 | //! |
| 62 | //! The configuration methods for [`FmtCollector`] can be found in |
| 63 | //! [`fmtBuilder`]. |
| 64 | //! |
| 65 | //! ## Formatters |
| 66 | //! |
| 67 | //! The output format used by the subscriber and collector 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, and span names are not shown; the |
| 79 | //! verbosity level is abbreviated to a single character. See |
| 80 | //! [here](format::Compact#example-output) for sample output. |
| 81 | //! |
| 82 | //! * [`format::Pretty`]: Emits excessively pretty, multi-line logs, optimized |
| 83 | //! for human readability. This is primarily intended to be used in local |
| 84 | //! development and debugging, or for command-line applications, where |
| 85 | //! automated analysis and compact storage of logs is less of a priority than |
| 86 | //! readability and visual appeal. See [here](format::Pretty#example-output) |
| 87 | //! for sample output. |
| 88 | //! |
| 89 | //! * [`format::Json`]: Outputs newline-delimited JSON logs. This is intended |
| 90 | //! for production use with systems where structured logs are consumed as JSON |
| 91 | //! by analysis and viewing tools. The JSON output is not optimized for human |
| 92 | //! readability. See [here](format::Json#example-output) for sample output. |
| 93 | //! |
| 94 | //! ### Customizing Formatters |
| 95 | //! |
| 96 | //! The formatting of log lines for spans and events is controlled by two |
| 97 | //! traits, [`FormatEvent`] and [`FormatFields`]. The [`FormatEvent`] trait |
| 98 | //! determines the overall formatting of the log line, such as what information |
| 99 | //! from the event's metadata and span context is included and in what order. |
| 100 | //! The [`FormatFields`] trait determines how fields — both the event's |
| 101 | //! fields and fields on spans — are formatted. |
| 102 | //! |
| 103 | //! The [`fmt::format`] module provides several types which implement these traits, |
| 104 | //! many of which expose additional configuration options to customize their |
| 105 | //! output. The [`format::Format`] type implements common configuration used by |
| 106 | //! all the formatters provided in this crate, and can be used as a builder to |
| 107 | //! set specific formatting settings. For example: |
| 108 | //! |
| 109 | //! ``` |
| 110 | //! use tracing_subscriber::fmt; |
| 111 | //! |
| 112 | //! // Configure a custom event formatter |
| 113 | //! let format = fmt::format() |
| 114 | //! .with_level(false) // don't include levels in formatted output |
| 115 | //! .with_target(false) // don't include targets |
| 116 | //! .with_thread_ids(true) // include the thread ID of the current thread |
| 117 | //! .with_thread_names(true) // include the name of the current thread |
| 118 | //! .compact(); // use the `Compact` formatting style. |
| 119 | //! |
| 120 | //! // Create a `fmt` collector that uses our custom event format, and set it |
| 121 | //! // as the default. |
| 122 | //! tracing_subscriber::fmt() |
| 123 | //! .event_format(format) |
| 124 | //! .init(); |
| 125 | //! ``` |
| 126 | //! |
| 127 | //! However, if a specific output format is needed, other crates can |
| 128 | //! also implement [`FormatEvent`] and [`FormatFields`]. See those traits' |
| 129 | //! documentation for details on how to implement them. |
| 130 | //! |
| 131 | //! ## Filters |
| 132 | //! |
| 133 | //! If you want to filter the `tracing` `Events` based on environment |
| 134 | //! variables, you can use the [`EnvFilter`] as follows: |
| 135 | //! |
| 136 | //! ```rust |
| 137 | //! use tracing_subscriber::EnvFilter; |
| 138 | //! |
| 139 | //! let filter = EnvFilter::from_default_env(); |
| 140 | //! ``` |
| 141 | //! |
| 142 | //! As mentioned above, the [`EnvFilter`] allows `Span`s and `Event`s to |
| 143 | //! be filtered at runtime by setting the `RUST_LOG` environment variable. |
| 144 | //! |
| 145 | //! You can find the other available [`filter`]s in the documentation. |
| 146 | //! |
| 147 | //! ### Using Your Collector |
| 148 | //! |
| 149 | //! Finally, once you have configured your `Collect`, you need to |
| 150 | //! configure your executable to use it. |
| 151 | //! |
| 152 | //! A collector can be installed globally using: |
| 153 | //! ```rust |
| 154 | //! use tracing; |
| 155 | //! use tracing_subscriber::fmt; |
| 156 | //! |
| 157 | //! let collector = fmt::Collector::new(); |
| 158 | //! |
| 159 | //! tracing::collect::set_global_default(collector) |
| 160 | //! .map_err(|_err| eprintln!("Unable to set global default collector")); |
| 161 | //! // Note this will only fail if you try to set the global default |
| 162 | //! // collector multiple times |
| 163 | //! ``` |
| 164 | //! |
| 165 | //! ## Composing Subscribers |
| 166 | //! |
| 167 | //! Composing an [`EnvFilter`] `Subscribe` and a [format `Subscribe`](super::fmt::Subscriber): |
| 168 | //! |
| 169 | //! ```rust |
| 170 | //! use tracing_subscriber::{fmt, EnvFilter}; |
| 171 | //! use tracing_subscriber::subscribe::CollectExt; |
| 172 | //! use tracing_subscriber::util::SubscriberInitExt; |
| 173 | //! |
| 174 | //! let fmt_subscriber = fmt::subscriber() |
| 175 | //! .with_target(false); |
| 176 | //! let filter_subscriber = EnvFilter::try_from_default_env() |
| 177 | //! .or_else(|_| EnvFilter::try_new("info")) |
| 178 | //! .unwrap(); |
| 179 | //! |
| 180 | //! tracing_subscriber::registry() |
| 181 | //! .with(filter_subscriber) |
| 182 | //! .with(fmt_subscriber) |
| 183 | //! .init(); |
| 184 | //! ``` |
| 185 | //! |
| 186 | //! [`EnvFilter`]: super::filter::EnvFilter |
| 187 | //! [`env_logger`]: https://docs.rs/env_logger/ |
| 188 | //! [`filter`]: super::filter |
| 189 | //! [`fmtBuilder`]: CollectorBuilder |
| 190 | //! [`FmtCollector`]: Collector |
| 191 | //! [`Collect`]: https://docs.rs/tracing/latest/tracing/trait.Collect.html |
| 192 | //! [`tracing`]: https://crates.io/crates/tracing |
| 193 | //! [`fmt::format`]: mod@crate::fmt::format |
| 194 | use std::{any::TypeId, error::Error, io, ptr::NonNull}; |
| 195 | use tracing_core::{collect::Interest, span, Event, Metadata}; |
| 196 | |
| 197 | mod fmt_subscriber; |
| 198 | #[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] |
| 199 | pub mod format; |
| 200 | #[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] |
| 201 | pub mod time; |
| 202 | #[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] |
| 203 | pub mod writer; |
| 204 | pub use fmt_subscriber::{FmtContext, FormattedFields, Subscriber}; |
| 205 | |
| 206 | use crate::subscribe::Subscribe as _; |
| 207 | use crate::{ |
| 208 | filter::LevelFilter, |
| 209 | registry::{LookupSpan, Registry}, |
| 210 | reload, subscribe, |
| 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 `Collector` that logs formatted representations of `tracing` events. |
| 221 | /// |
| 222 | /// This consists of an inner `Formatter` wrapped in a subscriber that performs filtering. |
| 223 | #[derive(Debug)] |
| 224 | #[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] |
| 225 | pub struct Collector< |
| 226 | N = format::DefaultFields, |
| 227 | E = format::Format, |
| 228 | F = LevelFilter, |
| 229 | W = fn() -> io::Stdout, |
| 230 | > { |
| 231 | inner: subscribe::Layered<F, Formatter<N, E, W>>, |
| 232 | } |
| 233 | |
| 234 | /// A collector 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<N = format::DefaultFields, E = format::Format, W = fn() -> io::Stdout> = |
| 238 | subscribe::Layered<fmt_subscriber::Subscriber<Registry, N, E, W>, Registry>; |
| 239 | |
| 240 | /// Configures and constructs `Collector`s. |
| 241 | #[derive(Debug)] |
| 242 | #[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] |
| 243 | #[must_use] |
| 244 | pub struct CollectorBuilder< |
| 245 | N = format::DefaultFields, |
| 246 | E = format::Format, |
| 247 | F = LevelFilter, |
| 248 | W = fn() -> io::Stdout, |
| 249 | > { |
| 250 | filter: F, |
| 251 | inner: Subscriber<Registry, N, E, W>, |
| 252 | } |
| 253 | |
| 254 | /// Returns a new [`CollectorBuilder`] for configuring a [formatting collector]. |
| 255 | /// |
| 256 | /// This is essentially shorthand for [`CollectorBuilder::default()`]. |
| 257 | /// |
| 258 | /// # Examples |
| 259 | /// |
| 260 | /// Using [`init`] to set the default collector: |
| 261 | /// |
| 262 | /// ```rust |
| 263 | /// tracing_subscriber::fmt().init(); |
| 264 | /// ``` |
| 265 | /// |
| 266 | /// Configuring the output format: |
| 267 | /// |
| 268 | /// ```rust |
| 269 | /// |
| 270 | /// tracing_subscriber::fmt() |
| 271 | /// // Configure formatting settings. |
| 272 | /// .with_target(false) |
| 273 | /// .with_timer(tracing_subscriber::fmt::time::uptime()) |
| 274 | /// .with_level(true) |
| 275 | /// // Set the collector as the default. |
| 276 | /// .init(); |
| 277 | /// ``` |
| 278 | /// |
| 279 | /// [`try_init`] returns an error if the default collector could not be set: |
| 280 | /// |
| 281 | /// ```rust |
| 282 | /// use std::error::Error; |
| 283 | /// |
| 284 | /// fn init_subscriber() -> Result<(), Box<dyn Error + Send + Sync + 'static>> { |
| 285 | /// tracing_subscriber::fmt() |
| 286 | /// // Configure the collector to emit logs in JSON format. |
| 287 | /// .json() |
| 288 | /// // Configure the collector to flatten event fields in the output JSON objects. |
| 289 | /// .flatten_event(true) |
| 290 | /// // Set the collector as the default, returning an error if this fails. |
| 291 | /// .try_init()?; |
| 292 | /// |
| 293 | /// Ok(()) |
| 294 | /// } |
| 295 | /// ``` |
| 296 | /// |
| 297 | /// Rather than setting the collector as the default, [`finish`] _returns_ the |
| 298 | /// constructed collector, which may then be passed to other functions: |
| 299 | /// |
| 300 | /// ```rust |
| 301 | /// let collector = tracing_subscriber::fmt() |
| 302 | /// .with_max_level(tracing::Level::DEBUG) |
| 303 | /// .compact() |
| 304 | /// .finish(); |
| 305 | /// |
| 306 | /// tracing::collect::with_default(collector, || { |
| 307 | /// // the collector will only be set as the default |
| 308 | /// // inside this closure... |
| 309 | /// }) |
| 310 | /// ``` |
| 311 | /// |
| 312 | /// [formatting collector]: Collector |
| 313 | /// [`CollectorBuilder::default()`]: struct.CollectorBuilder.html#method.default |
| 314 | /// [`init`]: CollectorBuilder::init() |
| 315 | /// [`try_init`]: CollectorBuilder::try_init() |
| 316 | /// [`finish`]: CollectorBuilder::finish() |
| 317 | #[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] |
| 318 | pub fn fmt() -> CollectorBuilder { |
| 319 | CollectorBuilder::default() |
| 320 | } |
| 321 | |
| 322 | /// Returns a new [formatting subscriber] that can be [composed] with other subscribers to |
| 323 | /// construct a collector. |
| 324 | /// |
| 325 | /// This is a shorthand for the equivalent [`Subscriber::default()`] function. |
| 326 | /// |
| 327 | /// [formatting subscriber]: Subscriber |
| 328 | /// [composed]: super::subscribe |
| 329 | /// [`Subscriber::default()`]: struct.Subscriber.html#method.default |
| 330 | #[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] |
| 331 | pub fn subscriber<C>() -> Subscriber<C> { |
| 332 | Subscriber::default() |
| 333 | } |
| 334 | |
| 335 | impl Collector { |
| 336 | /// The maximum [verbosity level] that is enabled by a `Collector` by |
| 337 | /// default. |
| 338 | /// |
| 339 | /// This can be overridden with the [`CollectorBuilder::with_max_level`] method. |
| 340 | /// |
| 341 | /// [verbosity level]: tracing_core::Level |
| 342 | pub const DEFAULT_MAX_LEVEL: LevelFilter = LevelFilter::INFO; |
| 343 | |
| 344 | /// Returns a new `CollectorBuilder` for configuring a format subscriber. |
| 345 | pub fn builder() -> CollectorBuilder { |
| 346 | CollectorBuilder::default() |
| 347 | } |
| 348 | |
| 349 | /// Returns a new format subscriber with the default configuration. |
| 350 | pub fn new() -> Self { |
| 351 | Default::default() |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | impl Default for Collector { |
| 356 | fn default() -> Self { |
| 357 | CollectorBuilder::default().finish() |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | // === impl Collector === |
| 362 | |
| 363 | impl<N, E, F, W> tracing_core::Collect for Collector<N, E, F, W> |
| 364 | where |
| 365 | N: for<'writer> FormatFields<'writer> + 'static, |
| 366 | E: FormatEvent<Registry, N> + 'static, |
| 367 | F: subscribe::Subscribe<Formatter<N, E, W>> + 'static, |
| 368 | W: for<'writer> MakeWriter<'writer> + 'static, |
| 369 | subscribe::Layered<F, Formatter<N, E, W>>: tracing_core::Collect, |
| 370 | fmt_subscriber::Subscriber<Registry, N, E, W>: subscribe::Subscribe<Registry>, |
| 371 | { |
| 372 | #[inline] |
| 373 | fn register_callsite(&self, meta: &'static Metadata<'static>) -> Interest { |
| 374 | self.inner.register_callsite(meta) |
| 375 | } |
| 376 | |
| 377 | #[inline] |
| 378 | fn enabled(&self, meta: &Metadata<'_>) -> bool { |
| 379 | self.inner.enabled(meta) |
| 380 | } |
| 381 | |
| 382 | #[inline] |
| 383 | fn new_span(&self, attrs: &span::Attributes<'_>) -> span::Id { |
| 384 | self.inner.new_span(attrs) |
| 385 | } |
| 386 | |
| 387 | #[inline] |
| 388 | fn record(&self, span: &span::Id, values: &span::Record<'_>) { |
| 389 | self.inner.record(span, values) |
| 390 | } |
| 391 | |
| 392 | #[inline] |
| 393 | fn record_follows_from(&self, span: &span::Id, follows: &span::Id) { |
| 394 | self.inner.record_follows_from(span, follows) |
| 395 | } |
| 396 | |
| 397 | #[inline] |
| 398 | fn event_enabled(&self, event: &Event<'_>) -> bool { |
| 399 | self.inner.event_enabled(event) |
| 400 | } |
| 401 | |
| 402 | #[inline] |
| 403 | fn event(&self, event: &Event<'_>) { |
| 404 | self.inner.event(event); |
| 405 | } |
| 406 | |
| 407 | #[inline] |
| 408 | fn enter(&self, id: &span::Id) { |
| 409 | // TODO: add on_enter hook |
| 410 | self.inner.enter(id); |
| 411 | } |
| 412 | |
| 413 | #[inline] |
| 414 | fn exit(&self, id: &span::Id) { |
| 415 | self.inner.exit(id); |
| 416 | } |
| 417 | |
| 418 | #[inline] |
| 419 | fn current_span(&self) -> span::Current { |
| 420 | self.inner.current_span() |
| 421 | } |
| 422 | |
| 423 | #[inline] |
| 424 | fn clone_span(&self, id: &span::Id) -> span::Id { |
| 425 | self.inner.clone_span(id) |
| 426 | } |
| 427 | |
| 428 | #[inline] |
| 429 | fn try_close(&self, id: span::Id) -> bool { |
| 430 | self.inner.try_close(id) |
| 431 | } |
| 432 | |
| 433 | #[inline] |
| 434 | fn max_level_hint(&self) -> Option<tracing_core::LevelFilter> { |
| 435 | self.inner.max_level_hint() |
| 436 | } |
| 437 | |
| 438 | unsafe fn downcast_raw(&self, id: TypeId) -> Option<NonNull<()>> { |
| 439 | if id == TypeId::of::<Self>() { |
| 440 | Some(NonNull::from(self).cast()) |
| 441 | } else { |
| 442 | self.inner.downcast_raw(id) |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | impl<'a, N, E, F, W> LookupSpan<'a> for Collector<N, E, F, W> |
| 448 | where |
| 449 | subscribe::Layered<F, Formatter<N, E, W>>: LookupSpan<'a>, |
| 450 | { |
| 451 | type Data = <subscribe::Layered<F, Formatter<N, E, W>> as LookupSpan<'a>>::Data; |
| 452 | |
| 453 | fn span_data(&'a self, id: &span::Id) -> Option<Self::Data> { |
| 454 | self.inner.span_data(id) |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | // ===== impl CollectorBuilder ===== |
| 459 | |
| 460 | impl Default for CollectorBuilder { |
| 461 | fn default() -> Self { |
| 462 | CollectorBuilder { |
| 463 | filter: Collector::DEFAULT_MAX_LEVEL, |
| 464 | inner: Default::default(), |
| 465 | } |
| 466 | .log_internal_errors(true) |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | impl<N, E, F, W> CollectorBuilder<N, E, F, W> |
| 471 | where |
| 472 | N: for<'writer> FormatFields<'writer> + 'static, |
| 473 | E: FormatEvent<Registry, N> + 'static, |
| 474 | W: for<'writer> MakeWriter<'writer> + 'static, |
| 475 | F: subscribe::Subscribe<Formatter<N, E, W>> + Send + Sync + 'static, |
| 476 | fmt_subscriber::Subscriber<Registry, N, E, W>: |
| 477 | subscribe::Subscribe<Registry> + Send + Sync + 'static, |
| 478 | { |
| 479 | /// Finish the builder, returning a new `FmtCollector`. |
| 480 | #[must_use = "you may want to use `try_init` or similar to actually install the collector."] |
| 481 | pub fn finish(self) -> Collector<N, E, F, W> { |
| 482 | let collector = self.inner.with_collector(Registry::default()); |
| 483 | Collector { |
| 484 | inner: self.filter.with_collector(collector), |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | /// Install this collector 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 collector 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 collector 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 collector was already installed by another call to `try_init`. |
| 513 | pub fn init(self) { |
| 514 | self.try_init().expect("Unable to install global collector") |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | impl<N, E, F, W> From<CollectorBuilder<N, E, F, W>> for tracing_core::Dispatch |
| 519 | where |
| 520 | N: for<'writer> FormatFields<'writer> + 'static, |
| 521 | E: FormatEvent<Registry, N> + 'static, |
| 522 | W: for<'writer> MakeWriter<'writer> + 'static, |
| 523 | F: subscribe::Subscribe<Formatter<N, E, W>> + Send + Sync + 'static, |
| 524 | fmt_subscriber::Subscriber<Registry, N, E, W>: |
| 525 | subscribe::Subscribe<Registry> + Send + Sync + 'static, |
| 526 | { |
| 527 | fn from(builder: CollectorBuilder<N, E, F, W>) -> tracing_core::Dispatch { |
| 528 | tracing_core::Dispatch::new(builder.finish()) |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | impl<N, L, T, F, W> CollectorBuilder<N, format::Format<L, T>, F, W> |
| 533 | where |
| 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) -> CollectorBuilder<N, format::Format<L, T2>, F, W> { |
| 551 | CollectorBuilder { |
| 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) -> CollectorBuilder<N, format::Format<L, ()>, F, W> { |
| 559 | CollectorBuilder { |
| 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 tracing_subscriber::fmt::format::FmtSpan; |
| 593 | /// use tracing_subscriber::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 `Collector`s or by |
| 602 | /// `Subscriber`s added to this subscriber. |
| 603 | /// |
| 604 | /// [lifecycle]: mod@tracing::span#the-span-lifecycle |
| 605 | /// [time]: CollectorBuilder::without_time() |
| 606 | pub fn with_span_events(self, kind: format::FmtSpan) -> Self { |
| 607 | CollectorBuilder { |
| 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) -> CollectorBuilder<N, format::Format<L, T>, F, W> { |
| 629 | CollectorBuilder { |
| 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::Collector` 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 | ) -> CollectorBuilder<N, format::Format<L, T>, F, W> { |
| 650 | CollectorBuilder { |
| 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 | ) -> CollectorBuilder<N, format::Format<L, T>, F, W> { |
| 661 | CollectorBuilder { |
| 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 | ) -> CollectorBuilder<N, format::Format<L, T>, F, W> { |
| 675 | CollectorBuilder { |
| 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 | ) -> CollectorBuilder<N, format::Format<L, T>, F, W> { |
| 689 | CollectorBuilder { |
| 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 | ) -> CollectorBuilder<N, format::Format<L, T>, F, W> { |
| 700 | CollectorBuilder { |
| 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 | ) -> CollectorBuilder<N, format::Format<L, T>, F, W> { |
| 714 | CollectorBuilder { |
| 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 | ) -> CollectorBuilder<N, format::Format<L, T>, F, W> { |
| 728 | CollectorBuilder { |
| 729 | inner: self.inner.with_thread_ids(display_thread_ids), |
| 730 | ..self |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | /// Sets the collector being built to use a less verbose formatter. |
| 735 | /// |
| 736 | /// See [`format::Compact`] for details. |
| 737 | pub fn compact(self) -> CollectorBuilder<N, format::Format<format::Compact, T>, F, W> |
| 738 | where |
| 739 | N: for<'writer> FormatFields<'writer> + 'static, |
| 740 | { |
| 741 | CollectorBuilder { |
| 742 | filter: self.filter, |
| 743 | inner: self.inner.compact(), |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | /// Sets the collector 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 | ) -> CollectorBuilder<format::Pretty, format::Format<format::Pretty, T>, F, W> { |
| 753 | CollectorBuilder { |
| 754 | filter: self.filter, |
| 755 | inner: self.inner.pretty(), |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | /// Sets the collector 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(self) -> CollectorBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> |
| 765 | where |
| 766 | N: for<'writer> FormatFields<'writer> + 'static, |
| 767 | { |
| 768 | CollectorBuilder { |
| 769 | filter: self.filter, |
| 770 | inner: self.inner.json(), |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | #[cfg(feature = "json")] |
| 776 | #[cfg_attr(docsrs, doc(cfg(feature = "json")))] |
| 777 | impl<T, F, W> CollectorBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> { |
| 778 | /// Sets the json collector being built to flatten event metadata. |
| 779 | /// |
| 780 | /// See [`format::Json`] for details. |
| 781 | pub fn flatten_event( |
| 782 | self, |
| 783 | flatten_event: bool, |
| 784 | ) -> CollectorBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> { |
| 785 | CollectorBuilder { |
| 786 | filter: self.filter, |
| 787 | inner: self.inner.flatten_event(flatten_event), |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | /// Sets whether or not the JSON subscriber being built will include the current span |
| 792 | /// in formatted events. |
| 793 | /// |
| 794 | /// See [`format::Json`] for details. |
| 795 | pub fn with_current_span( |
| 796 | self, |
| 797 | display_current_span: bool, |
| 798 | ) -> CollectorBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> { |
| 799 | CollectorBuilder { |
| 800 | filter: self.filter, |
| 801 | inner: self.inner.with_current_span(display_current_span), |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | /// Sets whether or not the JSON subscriber being built will include a list (from |
| 806 | /// root to leaf) of all currently entered spans in formatted events. |
| 807 | /// |
| 808 | /// See [`format::Json`] for details. |
| 809 | pub fn with_span_list( |
| 810 | self, |
| 811 | display_span_list: bool, |
| 812 | ) -> CollectorBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> { |
| 813 | CollectorBuilder { |
| 814 | filter: self.filter, |
| 815 | inner: self.inner.with_span_list(display_span_list), |
| 816 | } |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | impl<N, E, F, W> CollectorBuilder<N, E, reload::Subscriber<F>, W> |
| 821 | where |
| 822 | Formatter<N, E, W>: tracing_core::Collect + 'static, |
| 823 | { |
| 824 | /// Returns a `Handle` that may be used to reload the constructed collector's |
| 825 | /// filter. |
| 826 | pub fn reload_handle(&self) -> reload::Handle<F> { |
| 827 | self.filter.handle() |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | impl<N, E, F, W> CollectorBuilder<N, E, F, W> { |
| 832 | /// Sets the Visitor that the collector being built will use to record |
| 833 | /// fields. |
| 834 | /// |
| 835 | /// For example: |
| 836 | /// ```rust |
| 837 | /// use tracing_subscriber::fmt::format; |
| 838 | /// use tracing_subscriber::field::MakeExt; |
| 839 | /// |
| 840 | /// let formatter = |
| 841 | /// // Construct a custom formatter for `Debug` fields |
| 842 | /// format::debug_fn(|writer, field, value| write!(writer, "{}: {:?}", field, value)) |
| 843 | /// // Use the `tracing_subscriber::MakeExt` trait to wrap the |
| 844 | /// // formatter so that a delimiter is added between fields. |
| 845 | /// .delimited(", "); |
| 846 | /// |
| 847 | /// let collector = tracing_subscriber::fmt() |
| 848 | /// .fmt_fields(formatter) |
| 849 | /// .finish(); |
| 850 | /// # drop(collector) |
| 851 | /// ``` |
| 852 | pub fn fmt_fields<N2>(self, fmt_fields: N2) -> CollectorBuilder<N2, E, F, W> |
| 853 | where |
| 854 | N2: for<'writer> FormatFields<'writer> + 'static, |
| 855 | { |
| 856 | CollectorBuilder { |
| 857 | filter: self.filter, |
| 858 | inner: self.inner.fmt_fields(fmt_fields), |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | /// Sets the [`EnvFilter`] that the collector will use to determine if |
| 863 | /// a span or event is enabled. |
| 864 | /// |
| 865 | /// Note that this method requires the "env-filter" feature flag to be enabled. |
| 866 | /// |
| 867 | /// If a filter was previously set, or a maximum level was set by the |
| 868 | /// [`with_max_level`] method, that value is replaced by the new filter. |
| 869 | /// |
| 870 | /// # Examples |
| 871 | /// |
| 872 | /// Setting a filter based on the value of the `RUST_LOG` environment |
| 873 | /// variable: |
| 874 | /// ```rust |
| 875 | /// use tracing_subscriber::{fmt, EnvFilter}; |
| 876 | /// |
| 877 | /// fmt() |
| 878 | /// .with_env_filter(EnvFilter::from_default_env()) |
| 879 | /// .init(); |
| 880 | /// ``` |
| 881 | /// |
| 882 | /// Setting a filter based on a pre-set filter directive string: |
| 883 | /// ```rust |
| 884 | /// use tracing_subscriber::fmt; |
| 885 | /// |
| 886 | /// fmt() |
| 887 | /// .with_env_filter("my_crate=info,my_crate::my_mod=debug,[my_span]=trace") |
| 888 | /// .init(); |
| 889 | /// ``` |
| 890 | /// |
| 891 | /// Adding additional directives to a filter constructed from an env var: |
| 892 | /// ```rust |
| 893 | /// use tracing_subscriber::{fmt, filter::{EnvFilter, LevelFilter}}; |
| 894 | /// |
| 895 | /// # fn filter() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { |
| 896 | /// let filter = EnvFilter::try_from_env("MY_CUSTOM_FILTER_ENV_VAR")? |
| 897 | /// // Set the base level when not matched by other directives to WARN. |
| 898 | /// .add_directive(LevelFilter::WARN.into()) |
| 899 | /// // Set the max level for `my_crate::my_mod` to DEBUG, overriding |
| 900 | /// // any directives parsed from the env variable. |
| 901 | /// .add_directive("my_crate::my_mod=debug".parse()?); |
| 902 | /// |
| 903 | /// fmt() |
| 904 | /// .with_env_filter(filter) |
| 905 | /// .try_init()?; |
| 906 | /// # Ok(())} |
| 907 | /// ``` |
| 908 | /// [`EnvFilter`]: super::filter::EnvFilter |
| 909 | /// [`with_max_level`]: CollectorBuilder::with_max_level() |
| 910 | #[cfg(feature = "env-filter")] |
| 911 | #[cfg_attr(docsrs, doc(cfg(feature = "env-filter")))] |
| 912 | pub fn with_env_filter( |
| 913 | self, |
| 914 | filter: impl Into<crate::EnvFilter>, |
| 915 | ) -> CollectorBuilder<N, E, crate::EnvFilter, W> |
| 916 | where |
| 917 | Formatter<N, E, W>: tracing_core::Collect + 'static, |
| 918 | { |
| 919 | let filter = filter.into(); |
| 920 | CollectorBuilder { |
| 921 | filter, |
| 922 | inner: self.inner, |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | /// Sets the maximum [verbosity level] that will be enabled by the |
| 927 | /// collector. |
| 928 | /// |
| 929 | /// If the max level has already been set, or a [`EnvFilter`] was added by |
| 930 | /// [`with_env_filter`], this replaces that configuration with the new |
| 931 | /// maximum level. |
| 932 | /// |
| 933 | /// # Examples |
| 934 | /// |
| 935 | /// Enable up to the `DEBUG` verbosity level: |
| 936 | /// ```rust |
| 937 | /// use tracing_subscriber::fmt; |
| 938 | /// use tracing::Level; |
| 939 | /// |
| 940 | /// fmt() |
| 941 | /// .with_max_level(Level::DEBUG) |
| 942 | /// .init(); |
| 943 | /// ``` |
| 944 | /// This collector won't record any spans or events! |
| 945 | /// ```rust |
| 946 | /// use tracing_subscriber::{fmt, filter::LevelFilter}; |
| 947 | /// |
| 948 | /// let subscriber = fmt() |
| 949 | /// .with_max_level(LevelFilter::OFF) |
| 950 | /// .finish(); |
| 951 | /// ``` |
| 952 | /// [verbosity level]: tracing_core::Level |
| 953 | /// [`EnvFilter`]: struct@crate::filter::EnvFilter |
| 954 | /// [`with_env_filter`]: fn@Self::with_env_filter |
| 955 | pub fn with_max_level( |
| 956 | self, |
| 957 | filter: impl Into<LevelFilter>, |
| 958 | ) -> CollectorBuilder<N, E, LevelFilter, W> { |
| 959 | let filter = filter.into(); |
| 960 | CollectorBuilder { |
| 961 | filter, |
| 962 | inner: self.inner, |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | /// Configures the collector being built to allow filter reloading at |
| 967 | /// runtime. |
| 968 | /// |
| 969 | /// The returned builder will have a [`reload_handle`] method, which returns |
| 970 | /// a [`reload::Handle`] that may be used to set a new filter value. |
| 971 | /// |
| 972 | /// For example: |
| 973 | /// |
| 974 | /// ``` |
| 975 | /// use tracing::Level; |
| 976 | /// use tracing_subscriber::util::SubscriberInitExt; |
| 977 | /// |
| 978 | /// let builder = tracing_subscriber::fmt() |
| 979 | /// // Set a max level filter on the collector |
| 980 | /// .with_max_level(Level::INFO) |
| 981 | /// .with_filter_reloading(); |
| 982 | /// |
| 983 | /// // Get a handle for modifying the collector's max level filter. |
| 984 | /// let handle = builder.reload_handle(); |
| 985 | /// |
| 986 | /// // Finish building the collector, and set it as the default. |
| 987 | /// builder.finish().init(); |
| 988 | /// |
| 989 | /// // Currently, the max level is INFO, so this event will be disabled. |
| 990 | /// tracing::debug!("this is not recorded!"); |
| 991 | /// |
| 992 | /// // Use the handle to set a new max level filter. |
| 993 | /// // (this returns an error if the collector has been dropped, which shouldn't |
| 994 | /// // happen in this example.) |
| 995 | /// handle.reload(Level::DEBUG).expect("the collector should still exist"); |
| 996 | /// |
| 997 | /// // Now, the max level is INFO, so this event will be recorded. |
| 998 | /// tracing::debug!("this is recorded!"); |
| 999 | /// ``` |
| 1000 | /// |
| 1001 | /// [`reload_handle`]: CollectorBuilder::reload_handle |
| 1002 | /// [`reload::Handle`]: crate::reload::Handle |
| 1003 | pub fn with_filter_reloading(self) -> CollectorBuilder<N, E, reload::Subscriber<F>, W> { |
| 1004 | let (filter, _) = reload::Subscriber::new(self.filter); |
| 1005 | CollectorBuilder { |
| 1006 | filter, |
| 1007 | inner: self.inner, |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | /// Sets the [event formatter][`FormatEvent`] that the subscriber being built |
| 1012 | /// will use to format events that occur. |
| 1013 | /// |
| 1014 | /// The event formatter may be any type implementing the [`FormatEvent`] |
| 1015 | /// trait, which is implemented for all functions taking a [`FmtContext`], a |
| 1016 | /// [`Writer`], and an [`Event`]. |
| 1017 | /// |
| 1018 | /// # Examples |
| 1019 | /// |
| 1020 | /// Setting a type implementing [`FormatEvent`] as the formatter: |
| 1021 | /// |
| 1022 | /// ```rust |
| 1023 | /// use tracing_subscriber::fmt::format; |
| 1024 | /// |
| 1025 | /// let subscriber = tracing_subscriber::fmt() |
| 1026 | /// .event_format(format().compact()) |
| 1027 | /// .finish(); |
| 1028 | /// ``` |
| 1029 | /// |
| 1030 | /// [`Writer`]: struct@self::format::Writer |
| 1031 | pub fn event_format<E2>(self, fmt_event: E2) -> CollectorBuilder<N, E2, F, W> |
| 1032 | where |
| 1033 | E2: FormatEvent<Registry, N> + 'static, |
| 1034 | N: for<'writer> FormatFields<'writer> + 'static, |
| 1035 | W: for<'writer> MakeWriter<'writer> + 'static, |
| 1036 | { |
| 1037 | CollectorBuilder { |
| 1038 | filter: self.filter, |
| 1039 | inner: self.inner.event_format(fmt_event), |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | /// Sets the [`MakeWriter`] that the collector being built will use to write events. |
| 1044 | /// |
| 1045 | /// # Examples |
| 1046 | /// |
| 1047 | /// Using `stderr` rather than `stdout`: |
| 1048 | /// |
| 1049 | /// ```rust |
| 1050 | /// use tracing_subscriber::fmt; |
| 1051 | /// use std::io; |
| 1052 | /// |
| 1053 | /// fmt() |
| 1054 | /// .with_writer(io::stderr) |
| 1055 | /// .init(); |
| 1056 | /// ``` |
| 1057 | pub fn with_writer<W2>(self, make_writer: W2) -> CollectorBuilder<N, E, F, W2> |
| 1058 | where |
| 1059 | W2: for<'writer> MakeWriter<'writer> + 'static, |
| 1060 | { |
| 1061 | CollectorBuilder { |
| 1062 | filter: self.filter, |
| 1063 | inner: self.inner.with_writer(make_writer), |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | /// Configures the collector to support [`libtest`'s output capturing][capturing] when used in |
| 1068 | /// unit tests. |
| 1069 | /// |
| 1070 | /// See [`TestWriter`] for additional details. |
| 1071 | /// |
| 1072 | /// # Examples |
| 1073 | /// |
| 1074 | /// Using [`TestWriter`] to let `cargo test` capture test output. Note that we do not install it |
| 1075 | /// globally as it may cause conflicts. |
| 1076 | /// |
| 1077 | /// ```rust |
| 1078 | /// use tracing_subscriber::fmt; |
| 1079 | /// use tracing::collect; |
| 1080 | /// |
| 1081 | /// collect::set_default( |
| 1082 | /// fmt() |
| 1083 | /// .with_test_writer() |
| 1084 | /// .finish() |
| 1085 | /// ); |
| 1086 | /// ``` |
| 1087 | /// |
| 1088 | /// [capturing]: |
| 1089 | /// https://doc.rust-lang.org/book/ch11-02-running-tests.html#showing-function-output |
| 1090 | /// [`TestWriter`]: writer::TestWriter |
| 1091 | pub fn with_test_writer(self) -> CollectorBuilder<N, E, F, TestWriter> { |
| 1092 | CollectorBuilder { |
| 1093 | filter: self.filter, |
| 1094 | inner: self.inner.with_writer(TestWriter::default()), |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | /// Updates the event formatter by applying a function to the existing event formatter. |
| 1099 | /// |
| 1100 | /// This sets the event formatter that the collector being built will use to record fields. |
| 1101 | /// |
| 1102 | /// # Examples |
| 1103 | /// |
| 1104 | /// Updating an event formatter: |
| 1105 | /// |
| 1106 | /// ```rust |
| 1107 | /// let subscriber = tracing_subscriber::fmt() |
| 1108 | /// .map_event_format(|e| e.compact()) |
| 1109 | /// .finish(); |
| 1110 | /// ``` |
| 1111 | pub fn map_event_format<E2>(self, f: impl FnOnce(E) -> E2) -> CollectorBuilder<N, E2, F, W> |
| 1112 | where |
| 1113 | E2: FormatEvent<Registry, N> + 'static, |
| 1114 | N: for<'writer> FormatFields<'writer> + 'static, |
| 1115 | W: for<'writer> MakeWriter<'writer> + 'static, |
| 1116 | { |
| 1117 | CollectorBuilder { |
| 1118 | filter: self.filter, |
| 1119 | inner: self.inner.map_event_format(f), |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | /// Updates the field formatter by applying a function to the existing field formatter. |
| 1124 | /// |
| 1125 | /// This sets the field formatter that the subscriber being built will use to record fields. |
| 1126 | /// |
| 1127 | /// # Examples |
| 1128 | /// |
| 1129 | /// Updating a field formatter: |
| 1130 | /// |
| 1131 | /// ```rust |
| 1132 | /// use tracing_subscriber::field::MakeExt; |
| 1133 | /// let subscriber = tracing_subscriber::fmt() |
| 1134 | /// .map_fmt_fields(|f| f.debug_alt()) |
| 1135 | /// .finish(); |
| 1136 | /// ``` |
| 1137 | pub fn map_fmt_fields<N2>(self, f: impl FnOnce(N) -> N2) -> CollectorBuilder<N2, E, F, W> |
| 1138 | where |
| 1139 | N2: for<'writer> FormatFields<'writer> + 'static, |
| 1140 | { |
| 1141 | CollectorBuilder { |
| 1142 | filter: self.filter, |
| 1143 | inner: self.inner.map_fmt_fields(f), |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | /// Updates the [`MakeWriter`] by applying a function to the existing [`MakeWriter`]. |
| 1148 | /// |
| 1149 | /// This sets the [`MakeWriter`] that the subscriber being built will use to write events. |
| 1150 | /// |
| 1151 | /// # Examples |
| 1152 | /// |
| 1153 | /// Redirect output to stderr if level is <= WARN: |
| 1154 | /// |
| 1155 | /// ```rust |
| 1156 | /// use tracing::Level; |
| 1157 | /// use tracing_subscriber::fmt::{self, writer::MakeWriterExt}; |
| 1158 | /// |
| 1159 | /// let stderr = std::io::stderr.with_max_level(Level::WARN); |
| 1160 | /// let collector = tracing_subscriber::fmt() |
| 1161 | /// .map_writer(move |w| stderr.or_else(w)) |
| 1162 | /// .finish(); |
| 1163 | /// ``` |
| 1164 | pub fn map_writer<W2>(self, f: impl FnOnce(W) -> W2) -> CollectorBuilder<N, E, F, W2> |
| 1165 | where |
| 1166 | W2: for<'writer> MakeWriter<'writer> + 'static, |
| 1167 | { |
| 1168 | CollectorBuilder { |
| 1169 | filter: self.filter, |
| 1170 | inner: self.inner.map_writer(f), |
| 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | /// Install a global tracing collector that listens for events and |
| 1176 | /// filters based on the value of the [`RUST_LOG` environment variable], |
| 1177 | /// if one is not already set. |
| 1178 | /// |
| 1179 | /// If the `tracing-log` feature is enabled, this will also install |
| 1180 | /// the [`LogTracer`] to convert `log` records into `tracing` `Event`s. |
| 1181 | /// |
| 1182 | /// This is shorthand for |
| 1183 | /// |
| 1184 | /// ```rust |
| 1185 | /// # fn doc() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { |
| 1186 | /// tracing_subscriber::fmt().try_init() |
| 1187 | /// # } |
| 1188 | /// ``` |
| 1189 | /// |
| 1190 | /// |
| 1191 | /// # Errors |
| 1192 | /// |
| 1193 | /// Returns an Error if the initialization was unsuccessful, |
| 1194 | /// likely because a global collector was already installed by another |
| 1195 | /// call to `try_init`. |
| 1196 | /// |
| 1197 | /// [`LogTracer`]: |
| 1198 | /// https://docs.rs/tracing-log/0.1.0/tracing_log/struct.LogTracer.html |
| 1199 | /// [`RUST_LOG` environment variable]: |
| 1200 | /// ../filter/struct.EnvFilter.html#associatedconstant.DEFAULT_ENV |
| 1201 | pub fn try_init() -> Result<(), Box<dyn Error + Send + Sync + 'static>> { |
| 1202 | let builder = Collector::builder(); |
| 1203 | |
| 1204 | #[cfg(feature = "env-filter")] |
| 1205 | let builder = builder.with_env_filter(crate::EnvFilter::from_default_env()); |
| 1206 | |
| 1207 | builder.try_init() |
| 1208 | } |
| 1209 | |
| 1210 | /// Install a global tracing collector that listens for events and |
| 1211 | /// filters based on the value of the [`RUST_LOG` environment variable]. |
| 1212 | /// |
| 1213 | /// If the `tracing-log` feature is enabled, this will also install |
| 1214 | /// the LogTracer to convert `Log` records into `tracing` `Event`s. |
| 1215 | /// |
| 1216 | /// This is shorthand for |
| 1217 | /// |
| 1218 | /// ```rust |
| 1219 | /// tracing_subscriber::fmt().init() |
| 1220 | /// ``` |
| 1221 | /// |
| 1222 | /// # Panics |
| 1223 | /// Panics if the initialization was unsuccessful, likely because a |
| 1224 | /// global collector was already installed by another call to `try_init`. |
| 1225 | /// |
| 1226 | /// [`RUST_LOG` environment variable]: |
| 1227 | /// ../filter/struct.EnvFilter.html#associatedconstant.DEFAULT_ENV |
| 1228 | pub fn init() { |
| 1229 | try_init().expect("Unable to install global collector") |
| 1230 | } |
| 1231 | |
| 1232 | #[cfg(test)] |
| 1233 | mod test { |
| 1234 | use crate::{ |
| 1235 | filter::LevelFilter, |
| 1236 | fmt::{ |
| 1237 | format::{self, Format}, |
| 1238 | time, |
| 1239 | writer::MakeWriter, |
| 1240 | Collector, |
| 1241 | }, |
| 1242 | }; |
| 1243 | use std::{ |
| 1244 | io, |
| 1245 | sync::{Arc, Mutex, MutexGuard, TryLockError}, |
| 1246 | }; |
| 1247 | use tracing_core::dispatch::Dispatch; |
| 1248 | |
| 1249 | pub(crate) struct MockWriter { |
| 1250 | buf: Arc<Mutex<Vec<u8>>>, |
| 1251 | } |
| 1252 | |
| 1253 | impl MockWriter { |
| 1254 | pub(crate) fn new(buf: Arc<Mutex<Vec<u8>>>) -> Self { |
| 1255 | Self { buf } |
| 1256 | } |
| 1257 | |
| 1258 | pub(crate) fn map_error<Guard>(err: TryLockError<Guard>) -> io::Error { |
| 1259 | match err { |
| 1260 | TryLockError::WouldBlock => io::Error::from(io::ErrorKind::WouldBlock), |
| 1261 | TryLockError::Poisoned(_) => io::Error::from(io::ErrorKind::Other), |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | pub(crate) fn buf(&self) -> io::Result<MutexGuard<'_, Vec<u8>>> { |
| 1266 | self.buf.try_lock().map_err(Self::map_error) |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | impl io::Write for MockWriter { |
| 1271 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 1272 | self.buf()?.write(buf) |
| 1273 | } |
| 1274 | |
| 1275 | fn flush(&mut self) -> io::Result<()> { |
| 1276 | self.buf()?.flush() |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | #[derive(Clone, Default)] |
| 1281 | pub(crate) struct MockMakeWriter { |
| 1282 | buf: Arc<Mutex<Vec<u8>>>, |
| 1283 | } |
| 1284 | |
| 1285 | impl MockMakeWriter { |
| 1286 | pub(crate) fn new(buf: Arc<Mutex<Vec<u8>>>) -> Self { |
| 1287 | Self { buf } |
| 1288 | } |
| 1289 | |
| 1290 | // this is currently only used by the JSON formatter tests. if we need |
| 1291 | // it elsewhere in the future, feel free to remove the `#[cfg]` |
| 1292 | // attribute! |
| 1293 | #[cfg(feature = "json")] |
| 1294 | pub(crate) fn buf(&self) -> MutexGuard<'_, Vec<u8>> { |
| 1295 | self.buf.lock().unwrap() |
| 1296 | } |
| 1297 | |
| 1298 | pub(crate) fn get_string(&self) -> String { |
| 1299 | let mut buf = self.buf.lock().expect("lock shouldn't be poisoned"); |
| 1300 | let string = std::str::from_utf8(&buf[..]) |
| 1301 | .expect("formatter should not have produced invalid utf-8") |
| 1302 | .to_owned(); |
| 1303 | buf.clear(); |
| 1304 | string |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | impl<'a> MakeWriter<'a> for MockMakeWriter { |
| 1309 | type Writer = MockWriter; |
| 1310 | |
| 1311 | fn make_writer(&'a self) -> Self::Writer { |
| 1312 | MockWriter::new(self.buf.clone()) |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | #[test] |
| 1317 | fn impls() { |
| 1318 | let f = Format::default().with_timer(time::Uptime::default()); |
| 1319 | let subscriber = Collector::builder().event_format(f).finish(); |
| 1320 | let _dispatch = Dispatch::new(subscriber); |
| 1321 | |
| 1322 | let f = format::Format::default(); |
| 1323 | let subscriber = Collector::builder().event_format(f).finish(); |
| 1324 | let _dispatch = Dispatch::new(subscriber); |
| 1325 | |
| 1326 | let f = format::Format::default().compact(); |
| 1327 | let subscriber = Collector::builder().event_format(f).finish(); |
| 1328 | let _dispatch = Dispatch::new(subscriber); |
| 1329 | } |
| 1330 | |
| 1331 | #[test] |
| 1332 | fn subscriber_downcasts() { |
| 1333 | let subscriber = Collector::builder().finish(); |
| 1334 | let dispatch = Dispatch::new(subscriber); |
| 1335 | assert!(dispatch.downcast_ref::<Collector>().is_some()); |
| 1336 | } |
| 1337 | |
| 1338 | #[test] |
| 1339 | fn subscriber_downcasts_to_parts() { |
| 1340 | let subscriber = Collector::new(); |
| 1341 | let dispatch = Dispatch::new(subscriber); |
| 1342 | assert!(dispatch.downcast_ref::<format::DefaultFields>().is_some()); |
| 1343 | assert!(dispatch.downcast_ref::<LevelFilter>().is_some()); |
| 1344 | assert!(dispatch.downcast_ref::<format::Format>().is_some()) |
| 1345 | } |
| 1346 | |
| 1347 | #[test] |
| 1348 | fn is_lookup_span() { |
| 1349 | fn assert_lookup_span<T: for<'a> crate::registry::LookupSpan<'a>>(_: T) {} |
| 1350 | let subscriber = Collector::new(); |
| 1351 | assert_lookup_span(subscriber) |
| 1352 | } |
| 1353 | } |