Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colorize tail output

Tags:

linux

bash

shell

I've been trying to make tail a little more readable for server startups. My current command filters out most of the INFO and DEBUG messages from the startup:

tail -F ../server/durango/log/server.log | grep -e "ERROR" -e "WARN" -e "Shutdown" -e "MicroKernel" | grep --color=auto -E 'MicroKernel|$' 

What I would like to do is craft something that would highlight WARN in yellow and ERROR in red, and MicroKernel in green. I tried just piping grep --color=auto multiple times, but the only color that survives is the last command in the pipe.

Is there a one liner to do this? Or even a many-liner?

like image 235
tpederson Avatar asked Feb 04 '13 17:02

tpederson


People also ask

How do you color grep output?

The —-color parameter tells grep to color the search terms in the output, which helps them stand out when among all the other text on the line. You choose which color you want using the GREP_COLOR environment variable: export GREP_COLOR=36 gives you cyan, and export GREP_COLOR=32 gives you lime green.

What is MultiTail?

MultiTail is an open source ncurses utility that can be used to display multiple logfiles to standard output in a single window or a single shell that shows last few lines of logfiles in a real-time like tail command which split console into more subwindows (much like screen command).


1 Answers

yes, there is way to do this. That is, as long as your terminal supports ANSI escape sequences. This is most terminals that exist.

I think I don't need explain how to grep, sed etc. point is the color right?

see below, this will make

WARN yellow ERROR red foo   green 

here is example:

kent$ echo "WARN ERROR foo"|sed 's#WARN#\x1b[33m&#; s#ERROR#\x1b[31m&#; s#foo#\x1b[32m&#' 

Note: \x1b is hexadecimal for the ESC character (^VEsc).

to see the result:

enter image description here

like image 179
Kent Avatar answered Sep 30 '22 23:09

Kent