1 unstable release

Uses new Rust 2024

new 0.1.0 Apr 23, 2025

#717 in Filesystem

MIT license

12KB
229 lines

d_logger

A rust crate providing simple logging with a dated clean up util.

Examples

Create a logger with 7 days of cleaning

use d_logger::Logger;

let logger = Logger::new("C:/logs/".to_string(), "Log%d%m%y.log".to_string(), "%Y-%m-%d %H:%M:%S".to_string(), Some(7)).unwrap();
if !logger.write_log("This is a test log entry") {
    panic!("Logger failed");
}
// This will work
logger.log_clean(None);

Create a logger without any cleaning

use d_logger::Logger;

let logger = Logger::new("C:/logs/".to_string(), "Log%d%m%y.log".to_string(), "%Y-%m-%d %H:%M:%S".to_string(), None).unwrap();
if !logger.write_log("This is a test log entry") {
    panic!("Logger failed");
}
// This will fail
logger.log_clean(None);

Clean logs with a regex filter

use d_logger::Logger;

let logger = Logger::new("C:/logs/".to_string(), "Log%d%m%y.log".to_string(), "%Y-%m-%d %H:%M:%S".to_string(), Some(7)).unwrap();
if !logger.write_log("This is a test log entry") {
    panic!("Logger failed");
}
// This will work
logger.log_clean(Some(r"example_test_\d{8}.log"));

Functions

new(path: String, file_name_format: String, line_date_format: String, days_keep: Option) -> Result<Logger, io::error>

Create a new logging structure and test that the path and file given can be accessed.

write_log(line: String) -> bool

Write a line to the log file. Will return whether its successful

log_clean(regex: Option<&str>)

Clean up the log directory. If regex is provided only files matching regex will be deleted. Will only delete anything if days_keep was set. The files delete must be older than the number of days wanted to keep.

Dependencies

~3–4.5MB
~71K SLoC