This repository has been archived on 2026-03-27. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
tracing/examples/examples/appender-multifile.rs
Hayden Stainsby bdbaf80073
examples: add note to examples that they are for tracing 0.2.0 (#3099)
It is not uncommon that users who are new to tracing look at the
examples in the `master` branch of the repository and find that they
don't compile. This is because they are examples which compile with the
code from the master branch, which is for the as yet unreleased tracing
0.2.0 ecosystem.

Users should instead go to the `v0.1.x` branch to find examples
compatible with the crates published on crates.io.

This change adds a doc-comment to the beginning of every example file
informing the user of this fact and suggesting that they check out the
`v0.1.x` branch instead.
2024-10-09 15:31:11 -04:00

34 lines
1.3 KiB
Rust

//! 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.
//!
//! This example demonstrates the use of multiple files with
//! `tracing-appender`'s `RollingFileAppender`
use tracing_appender::rolling;
use tracing_subscriber::fmt::writer::MakeWriterExt;
#[path = "fmt/yak_shave.rs"]
mod yak_shave;
fn main() {
// Log all `tracing` events to files prefixed with `debug`. Since these
// files will be written to very frequently, roll the log file every minute.
let debug_file = rolling::minutely("./logs", "debug");
// Log warnings and errors to a separate file. Since we expect these events
// to occur less frequently, roll that file on a daily basis instead.
let warn_file = rolling::daily("./logs", "warnings").with_max_level(tracing::Level::WARN);
let all_files = debug_file.and(warn_file);
tracing_subscriber::fmt()
.with_writer(all_files)
.with_ansi(false)
.with_max_level(tracing::Level::TRACE)
.init();
yak_shave::shave_all(6);
tracing::info!("sleeping for a minute...");
std::thread::sleep(std::time::Duration::from_secs(60));
tracing::info!("okay, time to shave some more yaks!");
yak_shave::shave_all(10);
}