-
Notifications
You must be signed in to change notification settings - Fork 734
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
appender: impl MakeWriter for RollingFileAppender (#1760)
## Motivation Currently, `tracing-appender`'s `RollingFileAppender` does not implement the `MakeWriter` trait. This means it can only be used by either wrapping it in `NonBlocking`, or by wrapping it in a `Mutex`. However, this shouldn't be strictly necessary, as `&File` implements `io::Write`. It should thus only be necessary to introduce locking when we are in the process of _rotating_ the log file. ## Solution This branch adds a `MakeWriter` implementation for `RollingFileAppender`. This is done by moving the file itself inside of an `RwLock`, so that a read lock is acquired to write to the file. This allows multiple threads to write to the file without contention. When the file needs to be rolled, the rolling thread acquires the write lock to replace the file. Acquiring the write lock is guarded by an atomic CAS on the timestamp, so that only a single thread will try to roll the file. This prevents other threads from immediately rolling the file _again_ when the write lock is released. I...should probably write tests for that, though. Signed-off-by: Eliza Weisman <eliza@buoyant.io>
- Loading branch information
Showing
6 changed files
with
286 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//! 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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.