Home > Bash, Computers, Linux, Perl, Programming > Bash Color Logs

Bash Color Logs

Simple little function that will tail a log and color the lines accordingly. With the option to exclude any lines with a given regex, currently does not work on active tails using the -f option. Copy it into your .bashrc or .bash_profile.

#####
## Program:
##    Log tail with color and option to remove lines
## Author:
##    Kyle Rizzo
##    lifeforce0 {at} gmail {dot} com
##    https://lifeforce4.wordpress.com
## Summary:
##    Simple little function that will tail a log and color
##    the lines accordingly. With the option to exclude any
##    lines with a given regex.
#####
ctail()
{
   if  [ $# -eq "3" ] ; then
      tail $1 $2 | eval "perl -pe 's/.*$3.*\n//g'" | perl -pe 's/^.*SEVERE.*$/\e[0;31;40m$&\e[0m/g; s/^.*FATAL.*$/\e[0;31;40m$&\e[0m/g; s/^.*ERROR.*$/\e[0;31;40m$&\e[0m/g; s/^.*CRIT.*$/\e[1;31;40m$&\e[0m/g; s/^.*WARN.*$/\e[1;33;40m$&\e[0m/g; s/^.*DEBUG.*$/\e[1;36;40m$&\e[0m/g; s/^.*INFO.*$/\e[0;32;40m$&\e[0m/g; s/^.*VERB.*$/\e[1;37;40m$&\e[0m/g';
   elif [ $# -eq "2" ]; then
      tail $1 $2 | perl -pe 's/^.*SEVERE.*$/\e[0;31;40m$&\e[0m/g; s/^.*FATAL.*$/\e[0;31;40m$&\e[0m/g; s/^.*ERROR.*$/\e[0;31;40m$&\e[0m/g; s/^.*CRIT.*$/\e[1;31;40m$&\e[0m/g; s/^.*WARN.*$/\e[1;33;40m$&\e[0m/g; s/^.*DEBUG.*$/\e[1;36;40m$&\e[0m/g; s/^.*INFO.*$/\e[0;32;40m$&\e[0m/g; s/^.*VERB.*$/\e[1;37;40m$&\e[0m/g';
   elif [ $# -eq "1" ]; then
      tail $1 | perl -pe 's/^.*SEVERE.*$/\e[0;31;40m$&\e[0m/g; s/^.*FATAL.*$/\e[0;31;40m$&\e[0m/g; s/^.*ERROR.*$/\e[0;31;40m$&\e[0m/g; s/^.*CRIT.*$/\e[1;31;40m$&\e[0m/g; s/^.*WARN.*$/\e[1;33;40m$&\e[0m/g; s/^.*DEBUG.*$/\e[1;36;40m$&\e[0m/g; s/^.*INFO.*$/\e[0;32;40m$&\e[0m/g; s/^.*VERB.*$/\e[1;37;40m$&\e[0m/g';
   else
      echo -en "Usage: \e[1;36;40mctail -f /Path/to/log/file\e[0m\n       \e[1;36;40mctail -f /Path/to/log/file excludeText\e[0m\n       \e[1;36;40mctail -100 mylog.txt '(SEVERE|FATAL|ERROR)'\e[0m\n       \e[0mNote: Removing lines will only work if you're not actively tailing a file with the \e[1;36;40m-f\e[0m option.\e[0m\n";
   fi
}

Create a temp log file to test each level.

for level in VERBOSE DEBUG INFO WARN CRITICAL ERROR CRIT FATAL WARNING VERB SEVERE;do tempdate=`date`; echo $tempdate $level Random text that is about the msg >> mylog.txt; tempnum=$RANDOM;sleep $((tempnum %= 9)); done
Advertisements
Categories: Bash, Computers, Linux, Perl, Programming
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: