16 releases

0.7.5 Jun 8, 2023
0.7.3 Mar 14, 2023
0.7.1 Oct 14, 2022
0.7.0-rc.0 Jul 27, 2022
0.1.1 Jul 27, 2019

#45 in Filesystem

Download history 5476/week @ 2023-11-21 7882/week @ 2023-11-28 7981/week @ 2023-12-05 7621/week @ 2023-12-12 6215/week @ 2023-12-19 4176/week @ 2023-12-26 7017/week @ 2024-01-02 6498/week @ 2024-01-09 8079/week @ 2024-01-16 10016/week @ 2024-01-23 11218/week @ 2024-01-30 11262/week @ 2024-02-06 14106/week @ 2024-02-13 15468/week @ 2024-02-20 12639/week @ 2024-02-27 14449/week @ 2024-03-05

58,834 downloads per month
Used in 14 crates (10 directly)

MIT license

67KB
1K SLoC

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

Limitations / known issues:

  • file-rotate assumes that no other process or user moves files around in the logging directory, but we want to find a way to support this

Following are some supplementary examples to get started.

Basic example

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

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

    // 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",
    AppendTimestamp::default(FileLimit::MaxFiles(3)),
    ContentLimit::Lines(3),
    Compression::None,
    None,
);

// 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

~1.3–8MB
~32K SLoC