2 min read

Tail it with color

So, when you tail too many files and you don't have time to configure your shell with custom coloring there is a simple way to tail with color. It's a simple and basic approach to see some colors on the lines you were searching for.

First things first, create a new sh file, I preferred "tailitymf.sh" though I'm not really good with names, and put some pre color options to it as below.

#!/bin/sh
shopt -s expand_aliases

alias grey-grep="GREP_COLOR='1;35;38' grep -E --color=always --line-buffered"
alias red-grep="GREP_COLOR='1;24;41' grep -E --color=always --line-buffered"
alias green-grep="GREP_COLOR='1;32;39' grep -E --color=always --line-buffered"
alias yellow-grep="GREP_COLOR='1;34;37' grep -E --color=always --line-buffered"
alias cyan-grep="GREP_COLOR='1;36' grep -E --color=always --line-buffered"

tail $@ | grey-grep "[A-Z][a-z][a-z]\s[0-9][0-9][0-9: ]+|$" | red-grep "Fatal|FATAL|ERROR|Error$" | yellow-grep "Warning|WARNING|NOTICE|Notice*|$" | awk '{print "\n",$0}'

After you successfully save your file, next step making it executable with chmod +x
so you can run it more easily and if you want you can just symlink it to /usr/bin.

This is where the coloring configured; it uses grep to color outputs. For colors you can check the gnu page and give a point to this person for a nice detailed explanation for coloring https://askubuntu.com/a/1042242
alias grey-grep="GREP_COLOR='1;35;38' grep -E --color=always --line-buffered"

256 color chart
256 color chart

Next step is defining some grep options so we can tail with these nice colors. You can use regular expressions to match things or simple keywords as below, then awk will print it out for you. You can add more colors and filters using pipes.

tail $@ | red-grep "Fatal|Error$" | yellow-grep "Warning$" | awk '{print "\n",$0}'

Not to forget, there are already very nice tools already developed by the community like you can use. For example, tailspin is a very nice example for this
https://github.com/bensadeh/tailspin. And if you are working with web apps and want an analyzer, you can check https://github.com/allinurl/goaccess.