Small Java program to monitor a log file and send an e-mail when a new line in the log file matches a regex.
In order for the program to run, you need to specify the following parameters as command line arguments:
- Input file (-i)
- Regex to use on every line (-m)
- SMTP mail server to use (-h)
- Recipient e-mail address (-r)
- Sender e-mail address (-s)
java -jar log-monitoring.jar -i test.log -m ".*ERROR.*|.*WARN.*" -h mail.krenger.local -s [email protected] -r [email protected]
This example will monitor the log file test.log and test each new line against the regex ".*ERROR.*|.*WARN.*" (basically "tail -f test.log | grep -e " on UNIX). If the line matches the regex, an e-mail will be sent to [email protected] via the SMTP server mail.krenger.local and with the sender address [email protected].