logforth-append-file 0.3.0

File appender for Logforth with optional rollover strategy.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
// Copyright 2024 FastLabs Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fs;
use std::fs::File;
use std::fs::Metadata;
use std::fs::OpenOptions;
use std::io;
use std::io::Write;
use std::num::NonZeroUsize;
use std::path::Path;
use std::path::PathBuf;
use std::str::FromStr;

use jiff::Zoned;
use jiff::civil::DateTime;
use logforth_core::Error;
use logforth_core::Trap;
use logforth_core::trap::BestEffortTrap;

use crate::clock::Clock;
use crate::rotation::Rotation;

/// A writer for rolling files.
#[derive(Debug)]
pub struct RollingFileWriter {
    state: State,
    writer: File,
}

impl Drop for RollingFileWriter {
    fn drop(&mut self) {
        if let Err(err) = self.writer.flush() {
            let err = Error::new("failed to flush file writer on dropped").set_source(err);
            self.state.trap.trap(&err);
        }
    }
}

impl Write for RollingFileWriter {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let now = self.state.clock.now();
        let writer = &mut self.writer;

        if self.state.should_rollover_on_date(&now) {
            self.state.current_filesize = 0;
            self.state.next_date_timestamp = self.state.rotation.next_date_timestamp(&now);
            let current = &self.state.this_date_timestamp;
            self.state.refresh_writer(current, writer);
        }

        if self.state.should_rollover_on_size() {
            self.state.current_filesize = 0;
            self.state.refresh_writer(&now, writer);
        }

        self.state.this_date_timestamp = now;

        writer
            .write(buf)
            .inspect(|&n| self.state.current_filesize += n)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.writer.flush()
    }
}

/// A builder for configuring [`RollingFileWriter`].
#[derive(Debug)]
pub struct RollingFileWriterBuilder {
    // required
    basedir: PathBuf,
    filename: String,

    // has default
    rotation: Rotation,
    filename_suffix: Option<String>,
    max_size: Option<NonZeroUsize>,
    max_files: Option<NonZeroUsize>,
    clock: Clock,
    trap: Box<dyn Trap>,
}

impl RollingFileWriterBuilder {
    /// Creates a new [`RollingFileWriterBuilder`].
    #[must_use]
    pub fn new(basedir: impl Into<PathBuf>, filename: impl Into<String>) -> Self {
        Self {
            basedir: basedir.into(),
            filename: filename.into(),
            rotation: Rotation::Never,
            filename_suffix: None,
            max_size: None,
            max_files: None,
            clock: Clock::DefaultClock,
            trap: Box::new(BestEffortTrap::default()),
        }
    }

    /// Set the trap for the rolling file writer.
    pub fn trap(mut self, trap: impl Into<Box<dyn Trap>>) -> Self {
        self.trap = trap.into();
        self
    }

    /// Set the rotation policy.
    #[must_use]
    pub fn rotation(mut self, rotation: Rotation) -> Self {
        self.rotation = rotation;
        self
    }

    /// Set the filename suffix.
    #[must_use]
    pub fn filename_suffix(mut self, suffix: impl Into<String>) -> Self {
        let suffix = suffix.into();
        self.filename_suffix = if suffix.is_empty() {
            None
        } else {
            Some(suffix)
        };
        self
    }

    /// Set the maximum number of log files to keep.
    #[must_use]
    pub fn max_log_files(mut self, n: NonZeroUsize) -> Self {
        self.max_files = Some(n);
        self
    }

    /// Set the maximum size of a log file in bytes.
    #[must_use]
    pub fn max_file_size(mut self, n: NonZeroUsize) -> Self {
        self.max_size = Some(n);
        self
    }

    #[cfg(test)]
    fn clock(mut self, clock: Clock) -> Self {
        self.clock = clock;
        self
    }

    /// Builds the [`RollingFileWriter`].
    pub fn build(self) -> Result<RollingFileWriter, Error> {
        let Self {
            basedir,
            rotation,
            filename,
            filename_suffix,
            max_size,
            max_files,
            clock,
            trap,
        } = self;

        if filename.is_empty() {
            return Err(Error::new("filename must not be empty"));
        }

        let (state, writer) = State::new(
            rotation,
            basedir,
            filename,
            filename_suffix,
            max_size,
            max_files,
            clock,
            trap,
        )?;

        Ok(RollingFileWriter { state, writer })
    }
}

#[derive(Debug)]
struct LogFile {
    filepath: PathBuf,
    metadata: Metadata,
    datetime: DateTime,
    count: usize,
}

// oldest is the least
fn compare_logfile(a: &LogFile, b: &LogFile) -> std::cmp::Ordering {
    match a.datetime.cmp(&b.datetime) {
        std::cmp::Ordering::Equal => {
            let a_rev = usize::MAX - a.count;
            let b_rev = usize::MAX - b.count;
            a_rev.cmp(&b_rev)
        }
        ord => ord,
    }
}

#[derive(Debug)]
struct State {
    log_dir: PathBuf,
    log_filename: String,
    log_filename_suffix: Option<String>,
    date_format: &'static str,
    rotation: Rotation,
    current_filesize: usize,
    this_date_timestamp: Zoned,
    next_date_timestamp: Option<usize>,
    max_size: Option<NonZeroUsize>,
    max_files: Option<NonZeroUsize>,
    clock: Clock,
    trap: Box<dyn Trap>,
}

impl State {
    #[allow(clippy::too_many_arguments)]
    fn new(
        rotation: Rotation,
        dir: impl AsRef<Path>,
        log_filename: String,
        log_filename_suffix: Option<String>,
        max_size: Option<NonZeroUsize>,
        max_files: Option<NonZeroUsize>,
        clock: Clock,
        trap: Box<dyn Trap>,
    ) -> Result<(Self, File), Error> {
        let now = clock.now();
        let log_dir = dir.as_ref().to_path_buf();
        fs::create_dir_all(&log_dir)
            .map_err(|err| Error::new("failed to create log directory").set_source(err))?;

        let mut state = State {
            log_dir,
            log_filename,
            log_filename_suffix,
            date_format: rotation.date_format(),
            current_filesize: 0,
            this_date_timestamp: clock.now(),
            next_date_timestamp: rotation.next_date_timestamp(&now),
            rotation,
            max_size,
            max_files,
            clock,
            trap,
        };

        let files = {
            let mut files = state.list_logfiles()?;
            files.sort_by(compare_logfile);
            files
        };

        let file = match files.last() {
            None => {
                // brand-new directory
                state.create_log_writer()?
            }
            Some(last) => {
                let filename = state.current_filename();
                if last.filepath != filename {
                    // for some reason, the `filename.suffix` file does not exist; create a new one
                    state.create_log_writer()?
                } else {
                    state.current_filesize = last.metadata.len() as usize;

                    if let Ok(mtime) = last.metadata.modified() {
                        if let Ok(mtime) = Zoned::try_from(mtime) {
                            state.next_date_timestamp = state.rotation.next_date_timestamp(&mtime);
                            state.this_date_timestamp = mtime;
                        }
                    }

                    // continue to use the existing current log file
                    OpenOptions::new()
                        .append(true)
                        .open(&filename)
                        .map_err(|err| Error::new("failed to open current log").set_source(err))?
                }
            }
        };

        Ok((state, file))
    }

    fn current_filename(&self) -> PathBuf {
        let filename = &self.log_filename;
        match self.log_filename_suffix.as_ref() {
            None => self.log_dir.join(filename),
            Some(suffix) => self.log_dir.join(format!("{filename}.{suffix}")),
        }
    }

    fn create_log_writer(&self) -> Result<File, Error> {
        let filename = self.current_filename();
        OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&filename)
            .map_err(|err| Error::new("failed to create log file").set_source(err))
    }

    fn join_date(&self, date: &Zoned, cnt: usize) -> PathBuf {
        let date = date.strftime(self.date_format);
        let filename = match (
            &self.rotation,
            &self.log_filename,
            &self.log_filename_suffix,
        ) {
            (&Rotation::Never, filename, None) => format!("{filename}.{cnt}"),
            (&Rotation::Never, filename, Some(suffix)) => {
                format!("{filename}.{cnt}.{suffix}")
            }
            (_, filename, Some(suffix)) => format!("{filename}.{date}.{cnt}.{suffix}"),
            (_, filename, None) => format!("{filename}.{date}.{cnt}"),
        };
        self.log_dir.join(filename)
    }

    fn list_logfiles(&self) -> Result<Vec<LogFile>, Error> {
        let read_dir = fs::read_dir(&self.log_dir).map_err(|err| {
            Error::new(format!(
                "failed to read log dir: {}",
                self.log_dir.display()
            ))
            .set_source(err)
        })?;

        let files = read_dir
            .filter_map(|entry| {
                let entry = entry.ok()?;
                let filepath = entry.path();

                let metadata = entry.metadata().ok()?;
                // the appender only creates files, not directories or symlinks,
                if !metadata.is_file() {
                    return None;
                }

                let filename = entry.file_name();
                // if the filename is not a UTF-8 string, skip it.
                let mut filename = filename.to_str()?;
                if !filename.starts_with(&self.log_filename) {
                    return None;
                }
                filename = &filename[self.log_filename.len()..];

                if let Some(suffix) = &self.log_filename_suffix {
                    if !filename.ends_with(suffix) {
                        return None;
                    }
                    filename = &filename[..filename.len() - suffix.len() - 1];
                }

                if filename.is_empty() {
                    // the current log file is the largest
                    return Some(LogFile {
                        filepath,
                        metadata,
                        datetime: DateTime::MAX,
                        count: 0,
                    });
                }

                if filename.starts_with(".") {
                    filename = &filename[1..];
                } else {
                    return None;
                }

                let datetime = if self.rotation != Rotation::Never {
                    // mandatory datetime part
                    let pos = filename.find('.')?;
                    let datetime = DateTime::strptime(self.date_format, &filename[..pos]).ok()?;
                    filename = &filename[pos + 1..];
                    datetime
                } else {
                    DateTime::MAX
                };

                let count = usize::from_str(&filename[..filename.len()]).ok()?;

                Some(LogFile {
                    filepath,
                    metadata,
                    datetime,
                    count,
                })
            })
            .collect::<Vec<_>>();

        Ok(files)
    }

    fn delete_oldest_logs(&self, max_files: usize) -> Result<(), Error> {
        let mut files = self.list_logfiles()?;
        if files.len() < max_files {
            return Ok(());
        }

        // delete files, so that (n-1) files remain, because we will create another log file
        files.sort_by(compare_logfile);
        for file in files.iter().take(files.len() - (max_files - 1)) {
            let filepath = &file.filepath;
            fs::remove_file(filepath).map_err(|err| {
                Error::new(format!("failed to remove old log: {}", filepath.display()))
                    .set_source(err)
            })?;
        }

        Ok(())
    }

    fn rotate_log_writer(&self, now: &Zoned) -> Result<File, Error> {
        let mut renames = vec![];
        for i in 1..self.max_files.map_or(usize::MAX, |n| n.get()) {
            let filepath = self.join_date(now, i);
            if fs::exists(&filepath).is_ok_and(|ok| ok) {
                let next = self.join_date(now, i + 1);
                renames.push((filepath, next));
            } else {
                break;
            }
        }

        for (old, new) in renames.iter().rev() {
            fs::rename(old, new).map_err(|err| {
                Error::new(format!("failed to rotate log: {}", old.display())).set_source(err)
            })?
        }

        let archive_filepath = self.join_date(now, 1);
        let current_filepath = self.current_filename();
        fs::rename(&current_filepath, &archive_filepath).map_err(|err| {
            Error::new(format!(
                "failed to archive log: {}",
                current_filepath.display()
            ))
            .set_source(err)
        })?;

        if let Some(max_files) = self.max_files {
            if let Err(err) = self.delete_oldest_logs(max_files.get()) {
                let err = Error::new("failed to delete oldest logs").set_source(err);
                self.trap.trap(&err);
            }
        }

        self.create_log_writer()
    }

    fn refresh_writer(&self, now: &Zoned, file: &mut File) {
        match self.rotate_log_writer(now) {
            Ok(new_file) => {
                if let Err(err) = file.flush() {
                    let err = Error::new("failed to flush previous writer").set_source(err);
                    self.trap.trap(&err);
                }
                *file = new_file;
            }
            Err(err) => {
                let err = Error::new("failed to rotate log writer").set_source(err);
                self.trap.trap(&err);
            }
        }
    }

    fn should_rollover_on_date(&self, date: &Zoned) -> bool {
        self.next_date_timestamp
            .is_some_and(|ts| date.timestamp().as_millisecond() as usize >= ts)
    }

    fn should_rollover_on_size(&self) -> bool {
        self.max_size
            .is_some_and(|n| self.current_filesize >= n.get())
    }
}

#[cfg(test)]
mod tests {
    use std::cmp::min;
    use std::fs;
    use std::io::Write;
    use std::num::NonZeroUsize;
    use std::ops::Add;
    use std::str::FromStr;

    use jiff::Span;
    use jiff::Zoned;
    use rand::Rng;
    use rand::distr::Alphanumeric;
    use tempfile::TempDir;

    use crate::clock::Clock;
    use crate::clock::ManualClock;
    use crate::rolling::RollingFileWriterBuilder;
    use crate::rotation::Rotation;

    #[test]
    fn test_file_rolling_via_file_size() {
        test_file_rolling_for_specific_file_size(3, 1000);
        test_file_rolling_for_specific_file_size(3, 10000);
        test_file_rolling_for_specific_file_size(10, 8888);
        test_file_rolling_for_specific_file_size(10, 10000);
        test_file_rolling_for_specific_file_size(20, 6666);
        test_file_rolling_for_specific_file_size(20, 10000);
    }

    fn test_file_rolling_for_specific_file_size(max_files: usize, max_size: usize) {
        let max_files = NonZeroUsize::new(max_files).unwrap();
        let max_size = NonZeroUsize::new(max_size).unwrap();
        let temp_dir = TempDir::new().unwrap();

        let mut writer = RollingFileWriterBuilder::new(temp_dir.as_ref(), "test_file")
            .rotation(Rotation::Never)
            .filename_suffix("log")
            .max_log_files(max_files)
            .max_file_size(max_size)
            .build()
            .unwrap();

        for i in 1..=(max_files.get() * 2) {
            let mut expected_file_size = 0;
            while expected_file_size < max_size.get() {
                let rand_str = generate_random_string();
                expected_file_size += rand_str.len();
                assert_eq!(writer.write(rand_str.as_bytes()).unwrap(), rand_str.len());
                assert_eq!(writer.state.current_filesize, expected_file_size);
            }

            writer.flush().unwrap();
            assert_eq!(
                fs::read_dir(&writer.state.log_dir).unwrap().count(),
                min(i, max_files.get())
            );
        }
    }

    #[test]
    fn test_file_rolling_via_time_rotation() {
        test_file_rolling_for_specific_time_rotation(
            Rotation::Minutely,
            Span::new().minutes(1),
            Span::new().seconds(1),
        );
        test_file_rolling_for_specific_time_rotation(
            Rotation::Hourly,
            Span::new().hours(1),
            Span::new().minutes(1),
        );
        test_file_rolling_for_specific_time_rotation(
            Rotation::Daily,
            Span::new().days(1),
            Span::new().hours(1),
        );
    }

    fn test_file_rolling_for_specific_time_rotation(
        rotation: Rotation,
        rotation_duration: Span,
        write_interval: Span,
    ) {
        let max_files = NonZeroUsize::new(10).unwrap();
        let temp_dir = TempDir::new().unwrap();

        let start_time = Zoned::from_str("2024-08-10T00:00:00[UTC]").unwrap();
        let mut writer = RollingFileWriterBuilder::new(temp_dir.as_ref(), "test_file")
            .rotation(rotation)
            .filename_suffix("log")
            .max_log_files(max_files)
            .clock(Clock::ManualClock(ManualClock::new(start_time.clone())))
            .build()
            .unwrap();

        let mut cur_time = start_time;

        for i in 1..=(max_files.get() * 2) {
            let mut expected_file_size = 0;
            let end_time = cur_time.add(rotation_duration);
            while cur_time < end_time {
                writer.state.clock.set_now(cur_time.clone());

                let rand_str = generate_random_string();
                expected_file_size += rand_str.len();

                assert_eq!(writer.write(rand_str.as_bytes()).unwrap(), rand_str.len());
                assert_eq!(writer.state.current_filesize, expected_file_size);

                cur_time = cur_time.add(write_interval);
            }

            writer.flush().unwrap();
            assert_eq!(
                fs::read_dir(&writer.state.log_dir).unwrap().count(),
                min(i, max_files.get())
            );
        }
    }

    #[test]
    fn test_file_rolling_via_file_size_and_time_rotation() {
        test_file_size_and_time_rotation_for_specific_time_rotation(
            Rotation::Minutely,
            Span::new().minutes(1),
            Span::new().seconds(1),
        );
        test_file_size_and_time_rotation_for_specific_time_rotation(
            Rotation::Hourly,
            Span::new().hours(1),
            Span::new().minutes(1),
        );
        test_file_size_and_time_rotation_for_specific_time_rotation(
            Rotation::Daily,
            Span::new().days(1),
            Span::new().hours(1),
        );
    }

    fn test_file_size_and_time_rotation_for_specific_time_rotation(
        rotation: Rotation,
        rotation_duration: Span,
        write_interval: Span,
    ) {
        let max_files = NonZeroUsize::new(10).unwrap();
        let file_size = NonZeroUsize::new(500).unwrap();
        // Small file size and too many files to ensure both of file size and time rotation can be
        // triggered.
        let total_files = 100;
        let temp_dir = TempDir::new().unwrap();

        let start_time = Zoned::from_str("2024-08-10T00:00:00[UTC]").unwrap();
        let mut writer = RollingFileWriterBuilder::new(temp_dir.as_ref(), "test_file")
            .rotation(rotation)
            .filename_suffix("log")
            .max_log_files(max_files)
            .max_file_size(file_size)
            .clock(Clock::ManualClock(ManualClock::new(start_time.clone())))
            .build()
            .unwrap();

        let mut cur_time = start_time;
        let mut end_time = cur_time.add(rotation_duration);
        let mut time_rotation_trigger = false;
        let mut file_size_rotation_trigger = false;

        for i in 1..=total_files {
            let mut expected_file_size = 0;
            loop {
                writer.state.clock.set_now(cur_time.clone());

                let rand_str = generate_random_string();
                expected_file_size += rand_str.len();

                assert_eq!(writer.write(rand_str.as_bytes()).unwrap(), rand_str.len());
                assert_eq!(writer.state.current_filesize, expected_file_size);

                cur_time = cur_time.add(write_interval);

                if cur_time >= end_time {
                    end_time = end_time.add(rotation_duration);
                    time_rotation_trigger = true;
                    break;
                }
                if expected_file_size >= file_size.get() {
                    file_size_rotation_trigger = true;
                    break;
                }
            }

            writer.flush().unwrap();
            assert_eq!(
                fs::read_dir(&writer.state.log_dir).unwrap().count(),
                min(i, max_files.get())
            );
        }
        assert!(file_size_rotation_trigger);
        assert!(time_rotation_trigger);
    }

    fn generate_random_string() -> String {
        let mut rng = rand::rng();
        let len = rng.random_range(50..=100);
        let random_string: String = std::iter::repeat(())
            .map(|()| rng.sample(Alphanumeric))
            .map(char::from)
            .take(len)
            .collect();

        random_string
    }
}