Rauneet / log-monitor

log-monitoring script
0 stars 0 forks source link

log-monitor script

This script monitors specified log files for new entries and counts occurrences of a user-defined regex pattern. It is useful for tracking specific errors or events in application logs in real-time.

Prerequisites

Dependencies

No external dependencies is required except the Python Standard Library.

Setup

Usuage

To use the script follow these steps:

  1. Open the terminal.
  2. Navigate to the directory which contains the script.
  3. Run the script with the following command format: python log-monitoring.py <log_file_path> <pattern_to_search>
    • <log_file_path> absolute or relative path of the log file which you need to monitor.
    • <pattern_to_search> Regex pattern to look for in the log file (e.g., "ERROR", "404", etc.). Example: python log-monitoring.py /path/to/your/logfile.log "error|ERROR|404"

Testing

  1. Start the script as described above.
  2. Append entries to your log file.
    • You can manually add entries that match and do not match the pattern to see if the script picks up and counts the entries
      correctly.Use the following command to append to a log file: echo "2024-01-01 12:00:00 ERROR Something went wrong" >> /path/to/your/logfile.log
  3. Observe the output
    • The script prints each line it reads and counts occurrences of the pattern, reporting the counts every 10 lines.
  4. interupt the script by pressing Ctrl+C to test graceful shutdown.

Logging