A Rust library to watch the log files.
Note: Tested only on Linux
- Automatically reloads log file when log rotated via renaming a log file into another (to be archived) and creation of a blank log file with the same name
- Calls callback function when new line to parse
First, add the following to your Cargo.toml
[dependencies]
logwatcher = "0.2.1"
Add to your code,
use logwatcher::{LogWatcherAction, LogWatcherEvent, LogWatcher};
Register the logwatcher, pass a closure and watch it!
let mut log_watcher = LogWatcher::register("/var/log/auth.log").unwrap();
log_watcher.watch(&mut move |result| {
match result {
Ok(event) => match event {
LogWatcherEvent::Line(line) => {
println!("Line {}", line);
}
LogWatcherEvent::LogRotation => {
println!("Logfile rotation");
}
},
Err(err) => {
println!("Error {}", err);
}
}
LogWatcherAction::None
});