#log-file #filesize #file #log #rotation #rotate

firo

Straightforward rotating file implementation

5 releases

0.1.4 Nov 5, 2024
0.1.3 Jul 18, 2024
0.1.2 Jul 18, 2024
0.1.1 Jul 16, 2024
0.1.0 Jul 16, 2024

#163 in Compression

Download history 26/week @ 2024-07-28 5/week @ 2024-08-04 13/week @ 2024-09-15 19/week @ 2024-09-22 9/week @ 2024-09-29 8/week @ 2024-10-06 21/week @ 2024-10-13 1/week @ 2024-10-20 1/week @ 2024-10-27 136/week @ 2024-11-03 14/week @ 2024-11-10

153 downloads per month

GPL-3.0 license

34KB
705 lines

GitHub Actions Workflow Status Crates.io Version docs.rs

Firo

firo is a straightforward crate, inspired by std::fs::File, providing the necessary APIs to implement rotating files.

It currently supports:

  • rotation trigger based on time
  • rotation trigger based on size
  • file compression (at rotation time)
  • maximum size limit, oldest files will start being deleted if the sum of all files sizes goes beyond the limit
  • transparently reading a rotating file

Usage Example

Basic

use firo::{File, OpenOptions, Trigger, Compression};
use std::io::Write;
use std::time::Duration;

let td = tempfile::tempdir().unwrap();

// we initialize a rotating file options
let mut f = File::options()
    // file will rotate every hours
    .trigger(Duration::from_secs(3600).into())
    // finally create the file
    .create_append(td.path().join("test.log")).unwrap();

// one can use File as std::fs::File
for _ in 0..1000 {
    writeln!(f, "rotating every hour").unwrap();
}

Advanced

Rotating file based on size, enabling compression and deleting old files when total size reaches a given threshold.

use firo::{File, OpenOptions, Trigger, Compression};
use huby::ByteSize;
use tempfile;
use std::io::{BufReader, BufRead, Write};

let td = tempfile::tempdir().unwrap();
let p = td.path().join("test.log");
// we initialize a rotating file options
let opts = File::options()
    // file will rotate when reaching 1 KB
    .trigger(ByteSize::from_kb(1).into())
    // gzip compression is enabled (at rotation time)
    .compression(Compression::Gzip)
    // we start deleting oldest file when total size > 1 GB
    .max_size(ByteSize::from_gb(1));

let mut f = opts
    // finally create the file
    .create_append(&p).unwrap();

// one can use File as std::fs::File
for i in 0..100_000 {
    writeln!(f, "{i}").unwrap();
}

// make sure there are no pending writes
f.sync().unwrap();

// one can also read a rotating file
let f = opts.open(p).unwrap();
// we check that file is actually made of several
// files on disk (just for the demo)
assert!(f.files_sorted_by_index().unwrap().len() > 1);
let reader = BufReader::new(f);
let mut lines = reader.lines();

for i in 0..100_000 {
    let line = lines.next().unwrap().unwrap();
    let cmp = line.parse::<u32>().unwrap();
    assert_eq!(i, cmp);
}

Dependencies

~2–11MB
~132K SLoC