async_log_watch
is a simple Rust library developed as a part of a personal project. It is designed to monitor log files and trigger an async callback whenever a new line is added to the file. The library allows users to easily integrate log file monitoring into their projects, with support for monitoring multiple log files simultaneously.
The primary motivation behind creating this library was to efficiently detect new log lines generated by tools like pm2
. The library is built using the async-std
(can use the tokio by adding tokio runtime feature) and the notify
crate for file system event monitoring.
Add async-log-watch
to your Cargo.toml
dependencies:
toml
[dependencies]
async-log-watch = {version = "0.1"}
```rust use asynclogwatch::{LogWatcher, LogError};
async fn main() -> Result<(), Box
let filepath = "~/.pm2/logs/r1-out.log";
log_watcher
.register(filepath, |line: String, err: Option<LogError>| async move {
if err.is_none() {
println!("New log line: {}", line);
} else {
eprintln!("{}", err.unwrap());
}
}, None)
.await;
log_watcher
.monitoring(std::time::Duration::from_secs(1))
.await?;
Ok(())
} ```
This crate allows you to use tokio
runtime featured in async-std
by specifying features in your Cargo.toml
. By default, it uses async-std
with the attributes
feature.
To use the crate with the default configuration, add the following line to your Cargo.toml
:
toml
async-log-watch = "0.1"
To use a specific Tokio configuration, specify the feature like this:
toml
my-crate = { version = "0.1", features = ["tokio1"] }
async-std
with the attributes
feature.async-std
with the attributes
and tokio1
features.async-std
with the attributes
and tokio02
features.async-std
with the attributes
and tokio03
features.Please note that you should only enable one of these features at a time.
thiserror
library. - file errors occurs in spawn. support tokio runtime - [x] ~~Add support for other async runtimes (tokio)~~ - [x] Add support tokio runtime features in async-std
Add filtering options to process specific log lines based on patterns - [x] Add filtering option
This project is licensed under the MIT License - see the LICENSE file for details.