Skip to main content

tracing_logfmt/formatter/
builder.rs

1use tracing::Subscriber;
2use tracing_subscriber::{
3    fmt::{format::FmtSpan, Layer, SubscriberBuilder},
4    registry::LookupSpan,
5};
6
7use crate::{EventsFormatter, FieldsFormatter};
8
9pub struct Builder {
10    events: EventsFormatter,
11    fields: FieldsFormatter,
12    span_events: FmtSpan,
13}
14
15/// Create a builder that can be used to configure the formatter.
16///
17/// Example:
18/// ```rust
19/// use tracing::dispatcher::{self, Dispatch};
20/// use tracing_subscriber::Registry;
21/// use tracing_subscriber::layer::SubscriberExt;
22///
23/// let subscriber = Registry::default()
24///     .with(tracing_logfmt::builder().with_span_path(false).layer());
25///
26/// dispatcher::set_global_default(Dispatch::new(subscriber))
27///     .expect("Global logger has already been set!");
28/// ```
29pub fn builder() -> Builder {
30    Builder::new()
31}
32
33impl Builder {
34    pub fn new() -> Self {
35        Self {
36            events: EventsFormatter::default(),
37            fields: FieldsFormatter::default(),
38            span_events: FmtSpan::NONE,
39        }
40    }
41
42    pub fn with_level(mut self, enable: bool) -> Self {
43        self.events.with_level = enable;
44        self
45    }
46    pub fn with_target(mut self, enable: bool) -> Self {
47        self.events.with_target = enable;
48        self
49    }
50    pub fn with_span_name(mut self, enable: bool) -> Self {
51        self.events.with_span_name = enable;
52        self
53    }
54    pub fn with_span_path(mut self, enable: bool) -> Self {
55        self.events.with_span_path = enable;
56        self
57    }
58    pub fn with_span_events(mut self, kind: FmtSpan) -> Self {
59        self.span_events = kind;
60        self
61    }
62    pub fn with_location(mut self, enable: bool) -> Self {
63        self.events.with_location = enable;
64        self
65    }
66    pub fn with_module_path(mut self, enable: bool) -> Self {
67        self.events.with_module_path = enable;
68        self
69    }
70    pub fn with_timestamp(mut self, enable: bool) -> Self {
71        self.events.with_timestamp = enable;
72        self
73    }
74
75    pub fn with_thread_names(mut self, enable: bool) -> Self {
76        self.events.with_thread_names = enable;
77        self
78    }
79
80    /// Enable emitting the thread id in log output.
81    ///
82    /// Note: On unsupported targets like WASM, the thread id will be `0`.
83    pub fn with_thread_ids(mut self, enable: bool) -> Self {
84        self.events.with_thread_ids = enable;
85        self
86    }
87    #[cfg(feature = "ansi_logs")]
88    pub fn with_ansi_color(mut self, enable: bool) -> Self {
89        self.events.with_ansi_color = enable;
90        self
91    }
92
93    pub fn layer<S>(self) -> Layer<S, FieldsFormatter, EventsFormatter>
94    where
95        S: Subscriber + for<'a> LookupSpan<'a>,
96    {
97        tracing_subscriber::fmt::layer()
98            .with_span_events(self.span_events)
99            .event_format(self.events)
100            .fmt_fields(self.fields)
101    }
102
103    pub fn subscriber_builder(self) -> SubscriberBuilder<FieldsFormatter, EventsFormatter> {
104        tracing_subscriber::fmt::Subscriber::builder()
105            .with_span_events(self.span_events)
106            .event_format(self.events)
107            .fmt_fields(self.fields)
108    }
109}
110
111impl Default for Builder {
112    fn default() -> Self {
113        Self::new()
114    }
115}