Code Monkey home page Code Monkey logo

file-rotate's Introduction

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, suffix::AppendCount};
use std::{fs, io::Write, path::PathBuf};

fn main() {
    let mut log = FileRotate::new("logs/log", AppendCount::new(2), ContentLimit::Lines(3), 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),
    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.

file-rotate's People

Contributors

ploppz avatar kstrafe avatar archis-polyverse avatar runfalk avatar mmponn avatar jb-alvarado avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.