Friday, April 10, 2015

How to make 'tail' command with color in SSH or Unix

While I was debugging the logs early in the morning for an application issue, it's hard to identify which is ERROR or INFO or the CLASS that I'm looking for when you're dealing with one color (i.e. green), it's hard. When remote and tailing a logs for debugging, it's not easy especially if you have a lot of application logs writing to your file. My approach in a way to make it understandable and easy to trace, I just change the color while tailing it, so that, it's easy for me to trace which one is ERROR or even the specific text based on your keyword or the name of your Java Class You may try the following command below and you can also experiment it.

Quick notes: /INFO/ and /ERROR/ are the keyword and you can replace it.

Command below will print all line with INFO in GREEN and all line with ERROR in RED

tail -1000f <log.file>| awk '/INFO/ {print "\033[32m" $0 "\033[39m"} /ERROR/ {print "\033[31m" $0 "\033[39m"}'

Command below will print all line in WHITE except for the line with ERROR. The line with ERROR will be print in RED.

tail -1000f <log.file>| awk '// {print "\033[37m" $0 "\033[39m"} /ERROR/ {print "\033[31m" $0 "\033[39m"}'

Please post your suggestion to make this "tail" and "awk" works better.

For Colors:
            
   30 - black   34 - blue          40 - black    44 - blue
   31 - red     35 - magenta       41 - red      45 - magenta
   32 - green   36 - cyan          42 - green    46 - cyan
   33 - yellow  37 - white         43 - yellow   47 - white


   

1 comment:

arcioneo said...

The only problem is other lines(the ones who does not contain INFO or ERROR) are not shown.

If that is the case I prefer grep --color