tracing-forest 0.1.2

Preserving contextual coherence among trace data from concurrent tasks
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Build the [`TreeLayer`] and [`Subscriber`] with custom configuration values.
//!
//! To start, call [`builder`] to create a [`LayerBuilder`], which configures
//! the [`TreeLayer`] by chaining methods.
//!
//! After the the layer is configured, call [`async_layer`] or
//! [`blocking_layer`] on the [`LayerBuilder`] to get a [`SubscriberBuilder`].
//! At this point, other [`Layer`]s can be composed onto it by chaining calls to
//! the [`with`] method.
//!
//! Finally, code can be run in the context of the [`Subscriber`] by calling
//! either [`on_future`] or [`on_closure`], depending on the type of
//! [`TreeLayer`] that was created.
//!
//! # Note
//!
//! If you don't need advanced configuration options, see
//! [`#[tracing_forest::test]`][crate::test] and
//! [`#[tracing_forest::main]`][crate::main].
//!
//! # Examples
//! Running asynchronously with a custom tag, writing to stderr, formatting with
//! pretty, and filtering out some logs.
//! ```
//! tracing_forest::declare_tags! {
//!     use tracing_forest::Tag;
//!
//!     #[derive(Tag)]
//!     pub(crate) enum BearTag {
//!         #[tag(lvl = "info", msg = "brown.bear", macro = "brown_bear")]
//!         BrownBear,
//!         #[tag(lvl = "warn", msg = "black.bear", macro = "black_bear")]
//!         BlackBear,
//!         #[tag(lvl = "error", msg = "polar.bear", macro = "polar_bear")]
//!         PolarBear
//!     }
//! }
//!
//! # #[test]
//! # fn test_builder() {
//! tracing_forest::builder()
//!     .pretty()
//!     .with_writer(std::io::stderr)
//!     .with_tag::<BearTag>()
//!     .blocking_layer()
//!     .with(tracing_subscriber::filter::LevelFilter::WARN)
//!     .on_closure(|| {
//!         brown_bear!("if it's brown get down");
//!         black_bear!("if it's black fight back");
//!         polar_bear!("if it's white good night");
//!     })
//! # }
//! ```
//! ```log
//! WARN     🚧 [black.bear]: if it's black fight back
//! ERROR    🚨 [polar.bear]: if it's white good night
//! ```
//!
//! See function level documentation of [`LayerBuilder`] and
//! [`SubscriberBuilder`] for details on the various configuration settings.
//!
//! [`async_layer`]: LayerBuilder::async_layer
//! [`blocking_layer`]: LayerBuilder::blocking_layer
//! [`with`]: SubscriberBuilder::with
//! [`on_future`]: SubscriberBuilder::on_future
//! [`on_closure`]: SubscriberBuilder::on_closure
use crate::formatter::{Formatter, Pretty};
use crate::processor::{BlockingProcessor, Processor};
use crate::tag::{NoTag, Tag};
use crate::{cfg_json, cfg_sync, TreeLayer};
cfg_json! {
    use crate::formatter::Json;
}
cfg_sync! {
    use crate::processor::AsyncProcessor;
    use std::future::Future;
    use std::io::Write;
}
use std::marker::PhantomData;
use tracing::Subscriber;
use tracing_subscriber::fmt::{MakeWriter, TestWriter};
use tracing_subscriber::layer::Layered;
use tracing_subscriber::{Layer, Registry};

/// Creates a new [`LayerBuilder`] to configure a [`Subscriber`] with a
/// [`TreeLayer`]. This is the prefered method for using a [`TreeLayer`].
///
/// See the [module level documentation] for details on using [`builder`].
///
/// [module level documentation]: self
pub fn builder() -> LayerBuilder<Pretty, fn() -> std::io::Stdout, NoTag> {
    LayerBuilder {
        formatter: Pretty::new(),
        make_writer: std::io::stdout,
        tag: PhantomData,
    }
}

/// A type for configuring [`TreeLayer`]s.
///
/// See the [module level documentation] for details on using [`LayerBuilder`].
///
/// [module level documentation]: self
pub struct LayerBuilder<F, W, T> {
    formatter: F,
    make_writer: W,
    tag: PhantomData<fn(T)>,
}

impl<F, W, T> LayerBuilder<F, W, T>
where
    F: 'static + Formatter + Send,
    W: 'static + for<'a> MakeWriter<'a> + Send,
    T: Tag,
{
    /// Applies a writer that is suitable for test environments.
    ///
    /// Configuration methods can be chained on the return value.
    ///
    /// # Examples
    /// ```
    /// tracing_forest::builder()
    ///     .with_test_writer()
    ///     .blocking_layer()
    ///     .on_closure(|| {
    ///         tracing::info!("Hello, world!");
    ///     })
    /// ```
    pub fn with_test_writer(self) -> LayerBuilder<F, TestWriter, T> {
        self.with_writer(TestWriter::new())
    }

    /// Applies the specified [`MakeWriter`].
    ///
    /// Configuration methods can be chained on the return value.
    ///
    /// # Examples
    /// ```
    /// tracing_forest::builder()
    ///     .with_writer(std::io::stderr)
    ///     .blocking_layer()
    ///     .on_closure(|| {
    ///         tracing::info!("Hello, world!");
    ///     })
    /// ```
    pub fn with_writer<W2>(self, make_writer: W2) -> LayerBuilder<F, W2, T>
    where
        W2: for<'a> MakeWriter<'a>,
    {
        LayerBuilder {
            formatter: self.formatter,
            make_writer,
            tag: self.tag,
        }
    }

    cfg_json! {
        /// Applies compact JSON formatting.
        ///
        /// Configuration methods can be chained on the return value.
        ///
        /// # Examples
        /// ```
        /// tracing_forest::builder()
        ///     .json()
        ///     .blocking_layer()
        ///     .on_closure(|| {
        ///         tracing::info!("Hello, world!");
        ///     })
        /// ```
        /// ```log
        /// {"level":"INFO","kind":{"Event":{"tag":null,"message":"Hello, world!","fields":{}}}}
        /// ```
        pub fn json(self) -> LayerBuilder<Json<true>, W, T> {
            self.with_formatter(Json::compact())
        }

        /// Applies pretty JSON formatting.
        ///
        /// Configuration methods can be chained on the return value.
        ///
        /// # Examples
        /// ```
        /// tracing_forest::builder()
        ///     .json_pretty()
        ///     .blocking_layer()
        ///     .on_closure(|| {
        ///         tracing::info!("Hello, world!");
        ///     })
        /// ```
        /// ```log
        /// {
        ///   "level": "INFO",
        ///   "kind": {
        ///     "Event": {
        ///       "tag": null,
        ///       "message": "Hello, world!",
        ///       "fields": {}
        ///     }
        ///   }
        /// }
        /// ```
        pub fn json_pretty(self) -> LayerBuilder<Json<false>, W, T> {
            self.with_formatter(Json::pretty())
        }
    }

    /// Applies pretty formatting.
    ///
    /// Configuration methods can be chained on the return value.
    ///
    /// # Examples
    /// ```
    /// tracing_forest::builder()
    ///     .json_pretty()
    ///     .blocking_layer()
    ///     .on_closure(|| {
    ///         tracing::info!("Hello, world!");
    ///     })
    /// ```
    /// ```log
    /// INFO     💬 [info]: Hello, world!
    /// ```
    pub fn pretty(self) -> LayerBuilder<Pretty, W, T> {
        self.with_formatter(Pretty::new())
    }

    /// Applies a custom [`Formatter`].
    ///
    /// Configuration methods can be chained on the return value.
    ///
    /// # Examples
    /// ```
    /// # use tracing_forest::formatter::Formatter;
    /// # use tracing_forest::layer::Tree;
    /// # use std::io::{self, Write};
    /// struct UselessFormatter;
    ///
    /// impl Formatter for UselessFormatter {
    ///     fn fmt(&self, _tree: Tree, writer: &mut Vec<u8>) -> io::Result<()> {
    ///         writeln!(writer, "I am useless")
    ///     }
    /// }
    ///
    /// tracing_forest::builder()
    ///     .with_formatter(UselessFormatter)
    ///     .blocking_layer()
    ///     .on_closure(|| {
    ///         tracing::info!("Hello, world!");
    ///     })
    /// ```
    /// ```log
    /// I am useless
    /// ```
    pub fn with_formatter<F2>(self, formatter: F2) -> LayerBuilder<F2, W, T> {
        LayerBuilder {
            formatter,
            make_writer: self.make_writer,
            tag: self.tag,
        }
    }

    /// Applies a custom [`Tag`].
    ///
    /// Configuration methods can be chained on the return value.
    ///
    /// # Examples
    /// ```
    /// tracing_forest::declare_tags! {
    ///     use tracing_forest::Tag;
    ///
    ///     #[derive(Tag)]
    ///     pub(crate) enum MyTag {
    ///         #[tag(lvl = "info", msg = "greeting", macro = "greeting")]
    ///         Greeting,
    ///     }
    /// }
    ///
    /// #[test]
    /// # fn test_with_tag() {
    /// tracing_forest::builder()
    ///     .with_tag::<MyTag>()
    ///     .blocking_layer()
    ///     .on_closure(|| {
    ///         greeting!("Hello, world!");
    ///     })
    /// # }
    /// ```
    /// ```log
    /// INFO     💬 [greeting]: Hello, world!
    /// ```
    pub fn with_tag<T2>(self) -> LayerBuilder<F, W, T2>
    where
        T2: Tag,
    {
        LayerBuilder {
            formatter: self.formatter,
            make_writer: self.make_writer,
            tag: PhantomData,
        }
    }

    /// Finalizes the layer to run with an [`AsyncProcessor`].
    ///
    /// # Examples
    /// ```
    /// # #[tokio::test]
    /// # async fn test_doc_write_async() {
    /// tracing_forest::builder()
    ///     .async_layer()
    ///     .on_future(async {
    ///         tracing::info!("Hello from Tokio");
    ///     })
    ///     .await
    /// # }
    /// ```
    #[cfg(feature = "sync")]
    #[cfg_attr(docsrs, doc(cfg(feature = "sync")))]
    pub fn async_layer(
        self,
    ) -> SubscriberBuilder<
        Layered<TreeLayer<AsyncProcessor>, Registry>,
        AsyncExtensions<impl Future<Output = ()>>,
    > {
        let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
        let processor = AsyncProcessor::from(tx);

        let handle = async move {
            while let Some(tree) = rx.recv().await {
                let mut buf = Vec::with_capacity(0);

                #[allow(clippy::expect_used)]
                {
                    self.formatter
                        .fmt(tree, &mut buf)
                        .expect("formatting failed");
                    self.make_writer
                        .make_writer()
                        .write_all(&buf[..])
                        .expect("writing failed");
                }
            }
        };

        let subscriber = processor.into_layer().tag::<T>().into_subscriber();

        SubscriberBuilder {
            subscriber,
            extensions: AsyncExtensions(handle),
        }
    }

    /// Finalizes the layer to run with a [`BlockingProcessor`].
    ///
    /// # Examples
    /// ```
    /// # fn main() {
    /// tracing_forest::builder()
    ///     .blocking_layer()
    ///     .on_closure(|| {
    ///         tracing::info!("Hello from the current thread");
    ///     })
    /// # }
    /// ```
    pub fn blocking_layer(
        self,
    ) -> SubscriberBuilder<Layered<TreeLayer<BlockingProcessor<F, W>>, Registry>, BlockingExtensions>
    {
        let processor = BlockingProcessor::new(self.formatter, self.make_writer);
        let subscriber = processor.into_layer().tag::<T>().into_subscriber();

        SubscriberBuilder {
            subscriber,
            extensions: BlockingExtensions,
        }
    }
}

/// Extensions for [`AsyncProcessor`].
pub struct AsyncExtensions<E>(E);

/// Extensions for [`BlockingProcessor`].
pub struct BlockingExtensions;

/// A type for building [`Subscriber`]s by composing many [`Layer`]s, while also
/// holding extension data necessary for some [`TreeLayer`] types.
pub struct SubscriberBuilder<S, E> {
    subscriber: S,
    extensions: E,
}

impl<S, E> SubscriberBuilder<S, E>
where
    S: Subscriber,
{
    /// Wraps the inner subscriber with the provided `layer`.
    ///
    /// # Examples
    /// ```
    /// tracing_forest::builder()
    ///     .blocking_layer()
    ///     .with(tracing_subscriber::filter::LevelFilter::INFO)
    ///     .on_closure(|| {
    ///         // do stuff here...
    ///     })
    /// ```
    pub fn with<L>(self, layer: L) -> SubscriberBuilder<Layered<L, S>, E>
    where
        L: Layer<S>,
    {
        SubscriberBuilder {
            subscriber: layer.with_subscriber(self.subscriber),
            extensions: self.extensions,
        }
    }
}

#[cfg(feature = "sync")]
#[cfg_attr(docsrs, doc(cfg(feature = "sync")))]
impl<S, E> SubscriberBuilder<S, AsyncExtensions<E>>
where
    S: Subscriber + Send + Sync,
    E: 'static + Future<Output = ()> + Send,
{
    /// Runs the provided future in the context of a subscriber layered with a
    /// [`TreeLayer`] and a [`tokio`] runtime.
    ///
    /// [`TreeLayer`]: crate::layer::TreeLayer
    pub async fn on_future(self, future: impl Future<Output = ()>) {
        let handle = tokio::spawn(self.extensions.0);
        let guard = tracing::subscriber::set_default(self.subscriber);
        future.await;
        drop(guard);
        handle.await.expect("failed closing the writing task");
    }
}

impl<S> SubscriberBuilder<S, BlockingExtensions>
where
    S: Subscriber + Send + Sync,
{
    /// Runs the provided closure in the context of a subscriber layered with a
    /// [`TreeLayer`].
    ///
    /// [`TreeLayer`]: crate::layer::TreeLayer
    pub fn on_closure<R>(self, closure: impl FnOnce() -> R) -> R {
        let _guard = tracing::subscriber::set_default(self.subscriber);
        closure()
    }
}