-
Notifications
You must be signed in to change notification settings - Fork 750
/
Copy pathfmt-multiple-writers.rs
34 lines (27 loc) · 1.33 KB
/
fmt-multiple-writers.rs
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
//! NOTE: This is pre-release documentation for the upcoming tracing 0.2.0 ecosystem. For the
//! release examples, please see the `v0.1.x` branch instead.
//!
//! An example demonstrating how `fmt::Subcriber` can write to multiple
//! destinations (in this instance, `stdout` and a file) simultaneously.
#[path = "fmt/yak_shave.rs"]
mod yak_shave;
use std::io;
use tracing_subscriber::{fmt, subscribe::CollectExt, EnvFilter};
fn main() {
let dir = tempfile::tempdir().expect("Failed to create tempdir");
let file_appender = tracing_appender::rolling::hourly(dir, "example.log");
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
let collector = tracing_subscriber::registry()
.with(EnvFilter::from_default_env().add_directive(tracing::Level::TRACE.into()))
.with(fmt::Subscriber::new().with_writer(io::stdout))
.with(fmt::Subscriber::new().with_writer(non_blocking));
tracing::collect::set_global_default(collector).expect("Unable to set a global collector");
let number_of_yaks = 3;
// this creates a new event, outside of any spans.
tracing::info!(number_of_yaks, "preparing to shave yaks");
let number_shaved = yak_shave::shave_all(number_of_yaks);
tracing::info!(
all_yaks_shaved = number_shaved == number_of_yaks,
"yak shaving completed."
);
}