This is why I love linux #372

Posted: 18 November 2008 by Jason Adams in Uncategorized
Tags: , , , ,

Let’s say you want to monitor the output of a log file while a process is running. One easy way to do that is tail -f my.log. As lines of text are passed into the log file, they will be displayed on the screen. Very handy! But let’s say this is a central log file for something like Ruby on Rails. Not only are the important logs that you want to track being sent to the log file, but so are a bajillion MySQL queries and other crap. The other crap is so overwhelming that the only way you can use the log file is by grepping it for some identifier you put in the important logs.

So I figured, why not combine them?

tail -f my.log | grep "my-identifier"

To be honest, I wasn’t even sure it would work… But sure enough, the magic happened. The log file is filtered and only the stuff you want to see is displayed.  Any other nifty ideas out there for doing log monitoring?


Comments
  1. Tailing more than one file and grepping:

    $ tail -f log1.log log2.log | egrep “log1.log|log2.log|WARN|ERROR”

    Another thing about tail (that’s often ignored) is that -f means to follow the file descriptor. The implications of this is that when the file roles over, you stop getting output, if you want to follow by name:

    $ tail –follow=name alog.log

    Or for some versions of tail:

    $ tail -F alog.log

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>