#log-file #logger #buffered #time #docs #rotate #implemetation

bin+lib buffered_logger

a file logger implemetation for crate log

3 stable releases

1.2.0 Feb 10, 2022
1.1.0 Dec 28, 2021
1.0.0 Dec 17, 2021

#11 in #rotate

MIT license

14KB
249 lines

buffered_logger

Crates.io Documentation

a file logger implemetation for crate log.


lib.rs:

buffered_logger

This is a file logger implemetation for crate log. It provides a buffer to save the log contents temporarily indeed of writing file every time. When the size of buffer exceeds the max buffer size, it writes the buffer to current log file. When the size of the current log file exceeds the max file size, the log file is rotated. It uses crate flate2 to compress rotatd file. It uses crate crossbeam-channel for multi-threading.

Usage

use buffered_logger::Logger;

// Initialize the logger and start the service.
let logger = Logger::init(log::Level::Trace, "logs/m.log".to_string(), 10, 1024, 1024 * 5, true).unwrap();
logger.start();

// Now you can start logging.
log::info!("this is an info message");
log::debug!("this is a debug message");

// Logger is clonable. This is useful for passing it to a different thread.
let logger_clone = logger.clone();

// You can manually write the buffer to current log file. eg. run it every second.
logger_clone.flush();

// You can manually rotate the log file. eg. run it every day at 00:00.
logger_clone.rotate();

Dependencies

~3.5–5MB
~84K SLoC