Skip to main content

tracing_subscriber_init/
lib.rs

1// Copyright (c) 2023 tracing-subscriber-init developers
2//
3// Licensed under the Apache License, Version 2.0
4// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. All files in the project carrying such notice may not be copied,
7// modified, or distributed except according to those terms.
8
9//! Convenience trait and functions to ease [`tracing-subscriber`][tracing-subscriber] initialization.
10//!
11//! Program configuration can come from multiple sources. This crate supplies the [`TracingConfig`] trait to allow the grouping
12//! of [`tracing-subscriber`][tracing-subscriber] initialization related items.
13//!
14//! For example, I often have some configuration from the command line (quiet and verbose flags),
15//! some configuration from a configuration file, and some configuration (secrets) loaded from external sources.  I implement this
16//! trait on a struct to collect the [`tracing-subscriber`][tracing-subscriber] related configuration, then use functions such as
17//! [`full_filtered`](crate::full_filtered) to configure layers as appropriate.
18//!
19//! There are also convenience functions such as [`set_default`](crate::set_default) that will
20//! setup a [`Registry`](tracing_subscriber::registry::Registry), add the given vector of [`Layer`](tracing_subscriber::Layer),
21//! and initialize per the upstream functions of the
22//! [same name](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/util/trait.SubscriberInitExt.html#method.set_default).
23//!
24//! [tracing-subscriber]: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/
25//! # Example
26//! ```rust
27//! # use anyhow::Result;
28//! # use std::fs::File;
29//! # use tracing::{info, Level, span};
30//! # use tracing_subscriber::{Layer, fmt::format::FmtSpan};
31//! # use tracing_subscriber_init::{TracingConfig, full, full_filtered, set_default};
32//! #
33//! # pub fn main() -> Result<()> {
34//! #[derive(Clone, Debug, Default)]
35//! struct TomlConfig {
36//!     // ...other configuration
37//!     tracing: Tracing,
38//!     tracing_file: TracingFile,
39//!     // ...other configuration
40//! }
41//!
42//! #[derive(Clone, Debug, Default)]
43//! struct Tracing {
44//!     target: bool,
45//!     thread_ids: bool,
46//!     thread_names: bool,
47//!     line_numbers: bool,
48//! }
49//!
50//! impl TracingConfig for Tracing {
51//!     // Normally pulled from command line arguments, i.e. prog -qq
52//!     fn quiet(&self) -> u8 {
53//!         0
54//!     }
55//!
56//!     // Normally pulled from command line arguments, i.e. prog -vv
57//!     fn verbose(&self) -> u8 {
58//!         2
59//!     }
60//!
61//!     fn with_line_number(&self) -> bool {
62//!         self.line_numbers
63//!     }
64//!
65//!     fn with_target(&self) -> bool {
66//!         self.target
67//!     }
68//!
69//!     fn with_thread_ids(&self) -> bool {
70//!         self.thread_ids
71//!     }
72//!
73//!     fn with_thread_names(&self) -> bool {
74//!         self.thread_names
75//!     }
76//! }
77//!
78//! #[derive(Clone, Debug, Default)]
79//! struct TracingFile;
80//!
81//! impl TracingConfig for TracingFile {
82//!     fn quiet(&self) -> u8 {
83//!         0
84//!     }
85//!
86//!     fn verbose(&self) -> u8 {
87//!         3
88//!     }
89//!
90//!     fn with_ansi(&self) -> bool {
91//!         false
92//!     }
93//! }
94//!
95//! // Load configuration and pull out the tracing specific.
96//! let toml_config = TomlConfig::default();
97//! let tracing_config = toml_config.tracing;
98//! let tracing_file_config = toml_config.tracing_file;
99//!
100//! // Setup a full format, filtered layer.  The filtering is set based on the quiet
101//! // and verbose values from the configuration
102//! let layer = full_filtered(&tracing_config);
103//!
104//! // Setup a second full format layer to write to a file.  Use the non-filtered
105//! // version when you wish to modify items such as the writer, or the time format.
106//! // You can also chose to ignore the generated level filter and apply your own.
107//! let file = File::create("trace.log")?;
108//! let (file_layer, level_filter) = full(&tracing_file_config);
109//! let file_layer = file_layer.with_writer(file).with_filter(level_filter);
110//!
111//! // Create a Registry, add the layers, and set this subscriber as the default
112//! // for this scope
113//! let _unused = set_default(vec![layer.boxed(), file_layer.boxed()]);
114//!
115//! // Create a new span and enter it.
116//! let span = span!(Level::INFO, "a new span");
117//! let _enter = span.enter();
118//!
119//! // Trace away...
120//! info!("info level");
121//! #    Ok(())
122//! # }
123//! ```
124
125// rustc lints
126#![cfg_attr(
127    all(feature = "unstable", nightly),
128    feature(
129        multiple_supertrait_upcastable,
130        must_not_suspend,
131        non_exhaustive_omitted_patterns_lint,
132        strict_provenance_lints,
133        unqualified_local_imports,
134    )
135)]
136#![cfg_attr(nightly, allow(single_use_lifetimes))]
137#![cfg_attr(
138    nightly,
139    deny(
140        absolute_paths_not_starting_with_crate,
141        ambiguous_glob_imports,
142        ambiguous_glob_reexports,
143        ambiguous_negative_literals,
144        ambiguous_wide_pointer_comparisons,
145        anonymous_parameters,
146        array_into_iter,
147        asm_sub_register,
148        async_fn_in_trait,
149        bad_asm_style,
150        bare_trait_objects,
151        boxed_slice_into_iter,
152        break_with_label_and_loop,
153        clashing_extern_declarations,
154        closure_returning_async_block,
155        coherence_leak_check,
156        confusable_idents,
157        const_evaluatable_unchecked,
158        const_item_mutation,
159        dangling_pointers_from_temporaries,
160        dead_code,
161        dependency_on_unit_never_type_fallback,
162        deprecated,
163        deprecated_in_future,
164        deprecated_safe_2024,
165        deprecated_where_clause_location,
166        deref_into_dyn_supertrait,
167        deref_nullptr,
168        double_negations,
169        drop_bounds,
170        dropping_copy_types,
171        dropping_references,
172        duplicate_macro_attributes,
173        dyn_drop,
174        edition_2024_expr_fragment_specifier,
175        elided_lifetimes_in_paths,
176        ellipsis_inclusive_range_patterns,
177        explicit_outlives_requirements,
178        exported_private_dependencies,
179        ffi_unwind_calls,
180        forbidden_lint_groups,
181        forgetting_copy_types,
182        forgetting_references,
183        for_loops_over_fallibles,
184        function_item_references,
185        hidden_glob_reexports,
186        if_let_rescope,
187        impl_trait_overcaptures,
188        impl_trait_redundant_captures,
189        improper_ctypes,
190        improper_ctypes_definitions,
191        inline_no_sanitize,
192        internal_features,
193        invalid_from_utf8,
194        invalid_macro_export_arguments,
195        invalid_nan_comparisons,
196        invalid_value,
197        irrefutable_let_patterns,
198        keyword_idents_2018,
199        keyword_idents_2024,
200        large_assignments,
201        late_bound_lifetime_arguments,
202        legacy_derive_helpers,
203        let_underscore_drop,
204        macro_use_extern_crate,
205        map_unit_fn,
206        meta_variable_misuse,
207        mismatched_lifetime_syntaxes,
208        missing_abi,
209        missing_copy_implementations,
210        missing_debug_implementations,
211        missing_docs,
212        missing_unsafe_on_extern,
213        mixed_script_confusables,
214        named_arguments_used_positionally,
215        never_type_fallback_flowing_into_unsafe,
216        no_mangle_generic_items,
217        non_ascii_idents,
218        non_camel_case_types,
219        non_contiguous_range_endpoints,
220        non_fmt_panics,
221        non_local_definitions,
222        non_shorthand_field_patterns,
223        non_snake_case,
224        non_upper_case_globals,
225        noop_method_call,
226        opaque_hidden_inferred_bound,
227        out_of_scope_macro_calls,
228        overlapping_range_endpoints,
229        path_statements,
230        private_bounds,
231        private_interfaces,
232        ptr_to_integer_transmute_in_consts,
233        redundant_imports,
234        redundant_lifetimes,
235        redundant_semicolons,
236        refining_impl_trait_internal,
237        refining_impl_trait_reachable,
238        renamed_and_removed_lints,
239        repr_transparent_non_zst_fields,
240        rust_2021_incompatible_closure_captures,
241        rust_2021_incompatible_or_patterns,
242        rust_2021_prefixes_incompatible_syntax,
243        rust_2021_prelude_collisions,
244        rust_2024_guarded_string_incompatible_syntax,
245        rust_2024_incompatible_pat,
246        rust_2024_prelude_collisions,
247        self_constructor_from_outer_item,
248        semicolon_in_expressions_from_macros,
249        single_use_lifetimes,
250        special_module_name,
251        stable_features,
252        static_mut_refs,
253        suspicious_double_ref_op,
254        tail_expr_drop_order,
255        trivial_bounds,
256        trivial_casts,
257        trivial_numeric_casts,
258        type_alias_bounds,
259        tyvar_behind_raw_pointer,
260        uncommon_codepoints,
261        unconditional_recursion,
262        uncovered_param_in_projection,
263        unexpected_cfgs,
264        unfulfilled_lint_expectations,
265        ungated_async_fn_track_caller,
266        uninhabited_static,
267        unit_bindings,
268        unknown_lints,
269        unknown_or_malformed_diagnostic_attributes,
270        unnameable_test_items,
271        unnameable_types,
272        unpredictable_function_pointer_comparisons,
273        unreachable_code,
274        unreachable_patterns,
275        unreachable_pub,
276        unsafe_attr_outside_unsafe,
277        unsafe_code,
278        unsafe_op_in_unsafe_fn,
279        unstable_name_collisions,
280        unstable_syntax_pre_expansion,
281        unused_allocation,
282        unused_assignments,
283        unused_associated_type_bounds,
284        unused_attributes,
285        unused_braces,
286        unused_comparisons,
287        unused_crate_dependencies,
288        unused_doc_comments,
289        unused_extern_crates,
290        unused_features,
291        unused_import_braces,
292        unused_imports,
293        unused_labels,
294        unused_lifetimes,
295        unused_macro_rules,
296        unused_macros,
297        unused_must_use,
298        unused_mut,
299        unused_parens,
300        unused_qualifications,
301        unused_results,
302        unused_unsafe,
303        unused_variables,
304        useless_ptr_null_checks,
305        uses_power_alignment,
306        variant_size_differences,
307        while_true,
308    )
309)]
310// If nightly and unstable, allow `incomplete_features` and `unstable_features`
311#![cfg_attr(
312    all(feature = "unstable", nightly),
313    allow(incomplete_features, unstable_features)
314)]
315// If nightly and not unstable, deny `incomplete_features` and `unstable_features`
316#![cfg_attr(
317    all(not(feature = "unstable"), nightly),
318    deny(incomplete_features, unstable_features)
319)]
320// The unstable lints
321#![cfg_attr(
322    all(feature = "unstable", nightly),
323    deny(
324        fuzzy_provenance_casts,
325        lossy_provenance_casts,
326        multiple_supertrait_upcastable,
327        must_not_suspend,
328        non_exhaustive_omitted_patterns,
329        unqualified_local_imports,
330    )
331)]
332// clippy lints
333#![cfg_attr(nightly, deny(clippy::all, clippy::pedantic))]
334// rustdoc lints
335#![cfg_attr(
336    nightly,
337    deny(
338        rustdoc::bare_urls,
339        rustdoc::broken_intra_doc_links,
340        rustdoc::invalid_codeblock_attributes,
341        rustdoc::invalid_html_tags,
342        rustdoc::missing_crate_level_docs,
343        rustdoc::private_doc_tests,
344        rustdoc::private_intra_doc_links,
345    )
346)]
347#![cfg_attr(all(docsrs, nightly), feature(doc_cfg))]
348
349mod config;
350mod format;
351mod initialize;
352mod utils;
353
354pub use self::config::Config as TracingConfig;
355pub use self::format::compact::compact;
356pub use self::format::compact::filtered as compact_filtered;
357pub use self::format::full::filtered as full_filtered;
358pub use self::format::full::full;
359#[cfg(feature = "json")]
360pub use self::format::json::filtered as json_filtered;
361#[cfg(feature = "json")]
362pub use self::format::json::json;
363pub use self::format::pretty::filtered as pretty_filtered;
364pub use self::format::pretty::pretty;
365pub use self::initialize::init;
366pub use self::initialize::set_default;
367pub use self::initialize::try_init;
368pub use self::utils::TestAll;
369pub use self::utils::get_effective_level;
370
371#[cfg(feature = "time")]
372#[doc(no_inline)]
373pub use time::format_description::well_known::Iso8601;
374#[cfg(feature = "time")]
375#[doc(no_inline)]
376pub use time::format_description::well_known::Rfc2822;
377#[cfg(feature = "time")]
378#[doc(no_inline)]
379pub use time::format_description::well_known::Rfc3339;
380#[cfg(feature = "tstime")]
381#[doc(no_inline)]
382pub use tracing_subscriber::Layer;
383#[cfg(feature = "tstime")]
384#[doc(no_inline)]
385pub use tracing_subscriber::fmt::time::OffsetTime;
386#[cfg(feature = "tstime")]
387#[doc(no_inline)]
388pub use tracing_subscriber::fmt::time::SystemTime;
389#[cfg(feature = "tstime")]
390#[doc(no_inline)]
391pub use tracing_subscriber::fmt::time::Uptime;
392#[cfg(feature = "tstime")]
393#[doc(no_inline)]
394pub use tracing_subscriber::fmt::time::UtcTime;