Skip to content

Commit 341d88f

Browse files
ozgunozerkCBenoithawkw
committed
appender: add option to automatically delete old log files (#2323)
## 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 <[email protected]> Co-authored-by: Eliza Weisman <[email protected]>
1 parent 4811e93 commit 341d88f

File tree

3 files changed

+368
-77
lines changed

3 files changed

+368
-77
lines changed

tracing-appender/Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ rust-version = "1.53.0"
2222

2323
[dependencies]
2424
crossbeam-channel = "0.5.6"
25-
time = { version = "0.3.2", default-features = false, features = ["formatting"] }
25+
time = { version = "0.3.2", default-features = false, features = ["formatting", "parsing"] }
2626
parking_lot = { optional = true, version = "0.12.1" }
2727
thiserror = "1"
2828

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

3535
[dev-dependencies]
36-
3736
criterion = { version = "0.3.6", default-features = false }
3837
tracing = { path = "../tracing", version = "0.1.35" }
39-
time = { version = "0.3.2", default-features = false, features = ["formatting", "parsing"] }
4038
tempfile = "3"
4139

4240
[[bench]]

0 commit comments

Comments
 (0)