#added #log #callback #log-line #monitor #trigger #async

async-log-watch

A simple Rust library to monitor log files and trigger an async callback when a new line is added

8 releases

0.2.0 May 7, 2023
0.1.9 May 6, 2023

#9 in #added

MIT license

25KB
353 lines

Async log watch

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.

Usage

Add async-log-watch to your Cargo.toml dependencies:

[dependencies]
async-log-watch = {version = "0.2"}

Example

For a example demonstrating the usage of this library, please refer to the example code in the examples folder.

Cargo Features

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:

async-log-watch = "0.2"

To use a specific Tokio configuration, specify the feature like this:

async-log-watch = { version = "0.2", features = ["tokio1"] }

Available Features

  • default: Uses async-std with the attributes feature.
  • tokio1: Uses async-std with the attributes and tokio1 features.
  • tokio02: Uses async-std with the attributes and tokio02 features.
  • tokio03: Uses async-std with the attributes and tokio03 features.

Please note that you should only enable one of these features at a time.

TODO

  • Implement basic log monitoring.
  • Support async callbacks
  • Allow monitoring multiple log files simultaneously
  • Update with new version of dependencies.
  • FIXED: When convert into absolute filepath, tilde('~') is used as folder name.
  • FIXED: At the first time, watcher read the first line.
  • Error handling for file read errors
  • Improve error handling with the thiserror library. - file errors occurs in spawn.
  • Notify error through callback
  • Added methods : stop_monitoring_file and change_file_path
  • FIXED: absolute path in added methods | test code
  • Added new object LogEvent that encapsulates the line, path and LogError object.
  • Update the callback function's arguments to include the functionalities.
    • It allows user to handle log file rotation in the callback function when receiving a file open error

support tokio runtime

  • Add support for other async runtimes (tokio)
  • Add support tokio runtime features in async-std

Add filtering options to process specific log lines based on patterns

  • Add filtering option

Future Works

  • Add support for File name pattern : automatically monitor files that match the specified pattern within a directory.
  • Add support for log file rotation
  • Add trace log

License

This project is licensed under the MIT License - see the LICENSE file for details.

Dependencies

~3–14MB
~193K SLoC