Skip to content

Commit 6db58bb

Browse files
committed
[0.31.4] Fix bug with cleanup_by_day, improve test
1 parent 2466a96 commit 6db58bb

File tree

4 files changed

+24
-27
lines changed

4 files changed

+24
-27
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this
66
project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [0.31.3] - 2025-09-22
8+
## [0.31.4] - 2025-09-23
99

1010
Add variant `Cleanup::KeepForDays(usize)` to have log files being deleted after a given number of days.
1111
Kudos goes to [Tunglies](https://github.com/Tunglies).
1212

13+
## [0.31.3] - 2025-09-22
14+
15+
Yanked.
16+
1317
## [0.31.2] - 2025-06-27
1418

1519
Add syslog-call based `SyslogWriter` connection

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "flexi_logger"
3-
version = "0.31.3"
3+
version = "0.31.4"
44
authors = ["emabee <[email protected]>"]
55
categories = ["development-tools::debugging"]
66
description = """

src/writers/file_log_writer/state/list_and_cleanup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub(crate) fn remove_or_compress_too_old_logfiles_impl(
9696
}
9797
Cleanup::KeepLogFiles(log_limit) => (log_limit, 0, 0),
9898

99-
Cleanup::KeepForDays(day_limit) => (0, day_limit, 0),
99+
Cleanup::KeepForDays(day_limit) => (usize::MAX, day_limit, 0),
100100

101101
#[cfg(feature = "compress")]
102102
Cleanup::KeepCompressedFiles(compress_limit) => (0, 0, compress_limit),

tests/test_cleanup_by_day_limit.rs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ fn test_cleanup_by_day_limit() {
55
use filetime::{set_file_mtime, FileTime};
66
use flexi_logger::{Cleanup, Criterion, Duplicate, FileSpec, Logger, Naming};
77
use log::*;
8-
use std::{fs, thread, time::Duration};
8+
use std::{
9+
fs, thread,
10+
time::{Duration, Instant, SystemTime},
11+
};
912

1013
let directory = test_utils::dir();
1114

@@ -21,47 +24,37 @@ fn test_cleanup_by_day_limit() {
2124
.start()
2225
.unwrap();
2326

24-
for i in 0..5 {
27+
// create four "full" log files (r00000, r00001, r00002, rCURRENT)
28+
for i in 0..12 {
2529
info!("log line {i}");
2630
}
2731

2832
let mut log_files: Vec<_> = fs::read_dir(&directory)
2933
.unwrap()
30-
.filter_map(|e| {
31-
let p = e.unwrap().path();
34+
.filter_map(|r_dir_entry| {
35+
let p = r_dir_entry.unwrap().path();
3236
if p.extension().map(|ext| ext == "log").unwrap_or(false) {
3337
Some(p)
3438
} else {
3539
None
3640
}
3741
})
3842
.collect();
39-
let two_days_ago = std::time::SystemTime::now() - Duration::from_secs(2 * 24 * 3600);
40-
let ft = FileTime::from_system_time(two_days_ago);
41-
for file in &log_files {
42-
if file.exists() {
43-
let _ = set_file_mtime(file, ft);
44-
}
45-
}
4643
log_files.sort();
47-
let old_file = log_files.first().expect("should have log files");
48-
let two_days_ago = std::time::SystemTime::now() - Duration::from_secs(2 * 24 * 3600);
49-
let ft = FileTime::from_system_time(two_days_ago);
50-
if old_file.exists() {
51-
set_file_mtime(old_file, ft).unwrap();
52-
}
5344

54-
for i in 0..3 {
55-
info!("trigger cleanup {i}");
56-
thread::sleep(Duration::from_millis(120));
57-
}
45+
// artificially age the first file...
46+
let first_file = log_files.first().unwrap();
47+
let two_days_ago = SystemTime::now() - Duration::from_secs(2 * 24 * 3600);
48+
set_file_mtime(first_file, FileTime::from_system_time(two_days_ago)).unwrap();
5849

59-
let start = std::time::Instant::now();
60-
while old_file.exists() && start.elapsed().as_secs() < 2 {
50+
// ...and ensure it gets deleted automatically with the next rotation
51+
info!("add line to trigger the cleanup");
52+
let start = Instant::now();
53+
while first_file.exists() && start.elapsed().as_secs() < 2 {
6154
thread::sleep(Duration::from_millis(50));
6255
}
6356
assert!(
64-
!old_file.exists(),
57+
!first_file.exists(),
6558
"old file should be deleted by day_limit cleanup"
6659
);
6760
}

0 commit comments

Comments
 (0)