Skip to content

Commit 84200e7

Browse files
committed
feat(logging): improve documentation for file rotation methods
1 parent 1c16d25 commit 84200e7

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

datadog-log/src/writers.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ impl CustomFileAppender {
4444
chrono::Local::now().format("%Y-%m-%d_%H-%M-%S").to_string()
4545
}
4646

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.
4751
fn build_rotated_path(&self, timestamp: &str) -> PathBuf {
4852
match (self.path.file_stem(), self.path.extension()) {
4953
(Some(stem), Some(ext)) => {
@@ -63,6 +67,11 @@ impl CustomFileAppender {
6367
}
6468
}
6569

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.
6675
fn rotate_if_needed(&mut self) -> io::Result<()> {
6776
if self.max_size > 0 && self.current_size >= self.max_size {
6877
self.current_file.flush()?;
@@ -86,6 +95,10 @@ impl CustomFileAppender {
8695
Ok(())
8796
}
8897

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.
89102
fn cleanup_old_files(&self, max_files: u64) -> io::Result<()> {
90103
if max_files == 0 {
91104
return Ok(());

0 commit comments

Comments
 (0)