Bash-fu part2: highlight important stuff in logs and Maven build

Some time ago when I was staring at the log files, not much fun by the way, I said to myself that it would be nice to have a way to highlight information that is more important than the rest. I know there is plethora of log analysis tools that allows you to filter on the log data, but I don’t want tool with GUI, I like terminal.

With using sed and terminal colors it is a piece of cake. Let me post here a snippet of my ~/.bashrc file and I’ll comment it later on.

##### </Maven colors>
# Mvn color ( 
# Formatting constants
export BOLD=$(tput bold)
export UNDERLINE_ON=$(tput smul)
export UNDERLINE_OFF=$(tput rmul)
export TEXT_BLACK=$(tput setaf 0)
export TEXT_RED=$(tput setaf 1)
export TEXT_GREEN=$(tput setaf 2)
export TEXT_YELLOW=$(tput setaf 3)
export TEXT_BLUE=$(tput setaf 4)
export TEXT_MAGENTA=$(tput setaf 5)
export TEXT_CYAN=$(tput setaf 6)
export TEXT_WHITE=$(tput setaf 7)
export TEXT_ORANGE=$(tput setaf 172)
export BACKGROUND_BLACK=$(tput setab 0)
export BACKGROUND_RED=$(tput setab 1)
export BACKGROUND_GREEN=$(tput setab 2)
export BACKGROUND_YELLOW=$(tput setab 3)
export BACKGROUND_BLUE=$(tput setab 4)
export BACKGROUND_MAGENTA=$(tput setab 5)
export BACKGROUND_CYAN=$(tput setab 6)
export BACKGROUND_WHITE=$(tput setab 7)
export RESET_FORMATTING=$(tput sgr0)
# Wrapper function for Maven's mvn command.
mvnColor() {
  mvn $@ | sed -e "s/\(\[INFO\]\ \[.*\)/${RESET_FORMATTING}\1${RESET_FORMATTING}/g" \
               -e "/\[INFO\]\ Building\ \(war\:\|jar\:\|ear\:\)/! s/\(\[INFO\]\ Building .*\)/${TEXT_BLUE}\1${RESET_FORMATTING}/g" \
               -e "s/\(\[INFO\]\ BUILD SUCCESSFUL\)/${TEXT_GREEN}\1${RESET_FORMATTING}/g" \
               -e "s/\(\[INFO\]\ BUILD FAILURE\)/${TEXT_RED}\1${RESET_FORMATTING}/g" \
               -e "s/\(\[WARNING\].*\)/${TEXT_YELLOW}\1${RESET_FORMATTING}/g" \
               -e "s/\(\[ERROR\].*\)/${TEXT_RED}\1${RESET_FORMATTING}/g" \
               -e "s/\(Caused by: .*\)/${BOLD}${TEXT_ORANGE}\1${RESET_FORMATTING}/g" \
               -e "s/\(org\.rhq\..*\)/${BOLD}\1${RESET_FORMATTING}/g" \
               -e "s/Tests run: \([^,]*\), Failures: \([^,]*\), Errors: \([^,]*\), Skipped: \([^,]*\)/${BOLD}${TEXT_GREEN}Tests run: \1${RESET_FORMATTING}, Failures: ${BOLD}${TEXT_RED}\2${RESET_FORMATTING}, Errors: ${BOLD}${TEXT_RED}\3${RESET_FORMATTING}, Skipped: ${BOLD}${TEXT_YELLOW}\4${RESET_FORMATTING}/g"
  echo -ne ${RESET_FORMATTING}
alias mvn="mvnColor"
##### </Maven colors>

logColor() {
  echo $@
  [ $# = 0 ] && exit
  $@ | sed -e "s/\(\ INFO\ \ .*\)/${RESET_FORMATTING}\1${RESET_FORMATTING}/g" \
           -e "s/\(\ WARN\ \ .*\)/${TEXT_YELLOW}\1${RESET_FORMATTING}/g" \
           -e "s/\(\ ERROR\ .*\)/${TEXT_RED}\1${RESET_FORMATTING}/g" \
           -e "s/\(org\.rhq\..*\)/${BOLD}\1${RESET_FORMATTING}/g" \
           -e "s/\(Caused by: .*\)/${BOLD}${TEXT_RED}\1${RESET_FORMATTING}/g"

alias serverLog="logColor tail -f $RHQ_HOME/dev-container/rhq-server/logs/server.log"

There are two functions that have a lot of in common and it certainly could be refactored not to repeat the common parts, but let’s leave it aside.

I think the code is pretty self explaining, but for sure. What it does is that it kind of sniffs on the output of the watched command (mvn or tailf) and it tries to replace some patterns with the same content but surrounded by color tags. So for instance if in the output of Maven build there is a line containing “Building jar”, it will be blue. Test failures, build failures, errors and causes of exceptions are red. Warnings yellow, successful tests green, etc. I also use a bold highlight for any lines with “org.rhq.” pattern. This is here, because in the long exception stack trace I want to recognize where it talks about the code I own (no 3rd parties noise).

Package highlighting FTW

Package highlighting FTW

The same principles apply for the second function for log highlighting. It has one small drawback, if I redirect the output to a file, there is a mess there because of the colors, but it is easily solvable.


I hope, it’ll help you to find problems more quickly. If you find this information helpful and want to support me, you can send some small coin to my BTC wallet 1MKHbfwSYrYs2gZpiRupoJhaEkuXq6XNLL.

puppy dog eyes



Red Hat software engineer working on RHQ project, Android user, chess player, juggler, geek

Posted in Java, Tools

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: