Much better apache log with some colors

When I have to debug some web application I often stared at the Apache log and say WTF!!
This is what the log might looks like:

log_01

The problem comes because every time I go into the web I get hundreds of unrelated log lines, a huge mess!

So one day I thought, could I improve that? My first idea was to add some color using grep like this:

tail -f /var/log/apache2/error.log | grep -E "ERROR|" --color

log_02

That is nicer because now I can see clearly errors in red.
Could that be better? Maybe yellow warnings?
The answer is yes, grep output can be concatenated to another grep and different colors can be used in every grep. Like this little script:

#!/bin/bash
shopt -s expand_aliases

alias grey-grep="GREP_COLOR='1;30' grep -E --color=always --line-buffered"
alias red-grep="GREP_COLOR='1;31' grep -E --color=always --line-buffered"
alias green-grep="GREP_COLOR='1;32' grep -E --color=always --line-buffered"
alias yellow-grep="GREP_COLOR='1;33' grep -E --color=always --line-buffered"
alias cyan-grep="GREP_COLOR='1;36' grep -E --color=always --line-buffered"

tail -1000f /var/log/apache2/error.log | grey-grep ".*PerformanceLogger.*|$" | cyan-grep "INFO|$" | yellow-grep "WARN|$" | red-grep "\[ERROR\].*|\[FATAL\].*|$" | green-grep "\*\*\*|$"

So the output looks like:

log_03

Which is really cool, errors and warnings are clearly highlighted and all the performance parts I don’t care about are in a dark gray that allows me to focus on what is going on.

Of course, real programmers don’t need colours, this is just for fun 😉

References