Add some color to your Jira logs!

Tired of the old black and white run-of-the-mill log files? Well have I got the solution for you!

Whats the point?

This is a simple perl snippet that can help you quickly identify important lines in your log files - specifically, I've made this for use for watching Atlassian Jira's tomcat logs, which uses the log4j Java logging module.

Since the log format is standardised in configuration files, its easy to write regular expressions to match certain patterns or words within each line (word-boundary).

We want to surround the word with bash color codes to set and reset text color and backgrounds, such as \e[1;33;41m to set bright, yellow text with a red background and \e[0m to reset to defaults. The perl substitution would then look like this:

s/\b(WARN)\b/\e[1;33;41m$&\e[0m/

You can see this in action quickly by running:

cat {jira_home}/logs/catalina.out | perl -pe
"s/\b(WARN)\b/\e[1;33;41m$&\e[0m/"

Terminal Colors

I should note that I'm using the IR_Black theme on my Mac OSX terminal. See this page for more info. Makes the colors a more pastel than you'll see below. You'll also need to have enabled colors in your bash profile.

The color code is made up of the following:

  1. Escape character '\e['
  2. A. Normal or bright (0 or 1)
  3. B. Text color (30-37)
  4. C. Background color (40-47)
  5. lowercase 'm' to signify a color code In the form:
\e[`A`;`B`;`C`m

The A, B and C sections are optional, but the '\e[' and trailing 'm' are required. See the following table for color combinations:

Black (_0) Red (_1) Green (_2) Yellow (_3) Blue (_4) Magenta (_5) Cyan (_6) White (_7)
Text (3_) 30 31 32 33 34 35 36 37
Background (4_) 40 41 42 43 44 45 46 47

Converting to a bash function

We can use multiple substitution expressions by joining them with the || operator. I've developed my function to highlight the keywords DEBUG (blue), INFO (green), WARN (yellow) and ERROR (red), and also highlight lines containing SEVERE and stacktraces in red (containing Exception or starting with 'at'). The entire function looks like this:

tailight() {
    tail -f -n 60 $1 \ 
    | perl -ple 's/\b(INFO)\b/\e[1;32m$&\e[0m/
    || s/\b(DEBUG)\b/\e[1;34m$&\e[0m/
    || s/\b(WARN)\b/\e[1;33m$&\e[0m/
    || s/\b(ERROR)\b/\e[1;31m$&\e[0m/
    || s/^(.*(SEVERE|Exception)|\sat\s).*/\e[1;31m$&\e[0m/'
}

An alternative for the cat command would be:

catlight() {
    cat $1 \ 
    | perl -ple 's/\b(INFO)\b/\e[1;32m$&\e[0m/
    || s/\b(DEBUG)\b/\e[1;34m$&\e[0m/
    || s/\b(WARN)\b/\e[1;33m$&\e[0m/
    || s/\b(ERROR)\b/\e[1;31m$&\e[0m/
    || s/^(.*(SEVERE|Exception)|\sat\s).*/\e[1;31m$&\e[0m/'
}

Inspirations

  1. IR_Black Terminal Theme
  2. Tail with color (UNIX discussion)
  3. Color Your Tail With Perl