Wednesday, February 23, 2011

Watching your log for a specific value

If you use Unix, you probably know about piping one command's output through another. You may also know that
tail -f somefile
will show you whatever gets added to the end of a file in real time.

While working with Rails, I do
tail -f log/development.log
all the time to watch how Rails processes my page requests. Today, I was looking for a specific error message to show up - a MissingTemplate error that a user had experienced**, and I was trying to reproduce it.

Since I didn't care about anything that was being logged at that moment other than the error I was looking for, it occurred to me to do this:

tail -f log/development.log | grep 'MissingTemplate'

As I poked around our site, I saw nothing. Until I encountered the error, and BAM - that one line popped into my terminal. Error case located!

Just one more way that chaining simple tools together can be very powerful.

**Actually, the user didn't see the MissingTemplate error; they got a friendly error message, as they should. But we got an email from Hoptoad with the actual error.