@@ -44,6 +44,10 @@ impl CustomFileAppender {
44
44
chrono:: Local :: now ( ) . format ( "%Y-%m-%d_%H-%M-%S" ) . to_string ( )
45
45
}
46
46
47
+ /// Build the rotated file path.
48
+ /// The rotated files names are appended with their rotated at timestamp.
49
+ /// The file extension is preserved.
50
+ /// If the file has no extension, the timestamp is appended without a dot.
47
51
fn build_rotated_path ( & self , timestamp : & str ) -> PathBuf {
48
52
match ( self . path . file_stem ( ) , self . path . extension ( ) ) {
49
53
( Some ( stem) , Some ( ext) ) => {
@@ -63,6 +67,11 @@ impl CustomFileAppender {
63
67
}
64
68
}
65
69
70
+ /// Rotate the file if it exceeds the maximum size.
71
+ /// If the file exceeds the maximum size, it will be renamed to a new file with a timestamp
72
+ /// and the current file will be closed.
73
+ /// If the maximum number of files is exceeded, the oldest rotated files will be deleted.
74
+ /// The rotated files names are appended with their rotated at timestamp.
66
75
fn rotate_if_needed ( & mut self ) -> io:: Result < ( ) > {
67
76
if self . max_size > 0 && self . current_size >= self . max_size {
68
77
self . current_file . flush ( ) ?;
@@ -86,6 +95,10 @@ impl CustomFileAppender {
86
95
Ok ( ( ) )
87
96
}
88
97
98
+ /// Cleanup old files when the maximum number of files is exceeded.
99
+ /// The files are sorted by timestamp (newest first) to ensure we keep the most recent files
100
+ /// and delete the oldest ones when cleanup is needed.
101
+ /// The current file is never deleted.
89
102
fn cleanup_old_files ( & self , max_files : u64 ) -> io:: Result < ( ) > {
90
103
if max_files == 0 {
91
104
return Ok ( ( ) ) ;
0 commit comments