8 releases

0.5.3 Jan 12, 2022
0.5.2 Jan 12, 2022
0.5.0 Dec 15, 2021
0.4.0 Dec 6, 2020
0.1.1 Jul 27, 2019

#98 in Debugging

Download history 79/week @ 2021-10-02 188/week @ 2021-10-09 272/week @ 2021-10-16 519/week @ 2021-10-23 1457/week @ 2021-10-30 2217/week @ 2021-11-06 2756/week @ 2021-11-13 3261/week @ 2021-11-20 3118/week @ 2021-11-27 2897/week @ 2021-12-04 3060/week @ 2021-12-11 2443/week @ 2021-12-18 1542/week @ 2021-12-25 2504/week @ 2022-01-01 2668/week @ 2022-01-08 2423/week @ 2022-01-15

9,393 downloads per month
Used in 3 crates (via safe_network)

MIT license

49KB
818 lines

file-rotate

Rotate files with configurable suffix.

Look to the docs for explanatory examples of all features, like:

  • Using count or timestamp as suffix
  • Age-based deletion of log files
  • Optional compression
  • Getting a list of log files

Following are some supplementary examples to get started.

Basic example

use file_rotate::{FileRotate, ContentLimit, suffix::CountSuffix};
use std::{fs, io::Write, path::PathBuf};

fn main() {
    let mut log = FileRotate::new("logs/log", CountSuffix::new(2), ContentLimit::Lines(3));

    // Write a bunch of lines
    writeln!(log, "Line 1: Hello World!");
    for idx in 2..=10 {
        writeln!(log, "Line {}", idx);
    }
}
$ ls logs
log  log.1  log.2

$ cat log.2 log.1 log
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10

Example with timestamp suffixes

let mut log = FileRotate::new(
    "logs/log",
    TimestampSuffix::default(FileLimit::MaxFiles(3)),
    ContentLimit::Lines(3),
);

// Write a bunch of lines
writeln!(log, "Line 1: Hello World!");
for idx in 2..=10 {
    std::thread::sleep(std::time::Duration::from_millis(200));
    writeln!(log, "Line {}", idx);
}
$ ls logs
log                  log.20210825T151133.1
log.20210825T151133  log.20210825T151134

$ cat logs/*
Line 10
Line 1: Hello World!
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9

The timestamp format (including the extra trailing .N) works by default so that the lexical ordering of filenames equals the chronological ordering. So it almost works perfectly with cat logs/*, except that log is smaller (lexically "older") than all the rest. This can of course be fixed with a more complex script to assemble the logs.

License

This project is licensed under the MIT license.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in file-rotate by you, shall be licensed as MIT, without any additional terms or conditions.

Dependencies

~260–520KB

Ƚa