Skip to content

Commit

Permalink
appender: add option to automatically delete old log files (#2323)
Browse files Browse the repository at this point in the history
## Motivation

`tracing-appender` does not have `Rotation` based on size yet. Also, it
doesn't have the feature of keeping the most recent `N` log files

I believe the second feature is more easy to implement, and also will
partially solve the `Rotation` based on size problem. Because people may
choose `hourly` or `daily` rotation based on their needs, and put an
extra boundary of `keep the last 5 files` for example. Of course it
won't handle all the edge cases for `Rotation` based on size. But it
will cover most of the scenarios. And also, it is a good feature to have
on its own :)

## Solution

Introduce another field called `max_files: Option<usize>` to the `Inner`
of `RollingFileAppender` struct. I managed to did not touch any of the
existing functions, so it **WON'T BE A BREAKING CHANGE**. Yay :)

The solution is, whenever the rotation should happen, the
`refresh_writer()` is called. So I embed the following logic into that
function:

1- check the log folder and detect the log files 2- if there are more
log files than the `max_files` amount 3- store the filenames in a
vector, and sort them by their dates (dates are already present in the
filename) 4- keep deleting the oldest ones, till we have desired amount
of log files in the log folder

P.S. this PR was opened before, but got closed since it would be easier
for the maintainers to target `master` branch instead of `v0.1.x` Also,
@CBenoit contributed to this PR, it would be great to give credit to him
:)

Co-authored-by: Benoît Cortier <bcortier@proton.me>
Co-authored-by: Eliza Weisman <eliza@buoyant.io>
  • Loading branch information
3 people committed Sep 24, 2022
1 parent 17a4662 commit 7d5c10a
Show file tree
Hide file tree
Showing 3 changed files with 368 additions and 77 deletions.
4 changes: 1 addition & 3 deletions tracing-appender/Cargo.toml
Expand Up @@ -22,7 +22,7 @@ rust-version = "1.53.0"

[dependencies]
crossbeam-channel = "0.5.6"
time = { version = "0.3.2", default-features = false, features = ["formatting"] }
time = { version = "0.3.2", default-features = false, features = ["formatting", "parsing"] }
parking_lot = { optional = true, version = "0.12.1" }
thiserror = "1"

Expand All @@ -33,10 +33,8 @@ default-features = false
features = ["fmt", "std"]

[dev-dependencies]

criterion = { version = "0.3.6", default-features = false }
tracing = { path = "../tracing", version = "0.1.35" }
time = { version = "0.3.2", default-features = false, features = ["formatting", "parsing"] }
tempfile = "3"

[[bench]]
Expand Down

0 comments on commit 7d5c10a

Please sign in to comment.