Wednesday, January 8, 2014

Using Tail with Grep to monitor and view log files

I recently needed to monitor for exceptions on my system logs while doing load testing.

The tail command returns output from the last part of the log file.  The -f option (follow) continues to read more output at the end of the file as it is growing. Using tail with the '-f' option alone was overwhelming because it shows all the log data when all I wanted to monitor are the logs for exceptions.

The perfect solution is to use use tail with grep. The grep command searches file(s) for specific text.

Here is the command I used:

tail -f SystemOut.log | grep excep

This command returns only the logs with the text 'excep' from the SystemOut.log file.

You can also use the -v option with grep to invert the sense of matching, to select only the non-matching lines.

Here's an example:

tail -f SystemOut.log | grep -v "Alert Notice"

This command will return all the the logs that does not have the text "Alert Notice" on it.

You can check out more options for these commands from these links: tailgrep.

You can find practical grep examples here.

No comments:

Post a Comment