Colour-coding your log output with sed

If your application is logging events to a file, chances are that you often find yourself using tail to monitor that file. If you are using a logging library, chances are that your logging events are classified in differing orders of severity. Wouldn’t it be nice to make severe errors readily apparent? You can, by using sed.

If you are using a colour-aware terminal, you can use control codes to change the text colour. By inserting these codes with a sed script, tail output can be colour-coded.

For example, supposing you wanted all lines labelled as containing severe errors to be displayed in bright red. This can be accomplished using the following line:

> tail -f mylog.log | sed -e "s/\(.*SEVERE.*\)/^[[1;31m\1^[[0m/g"

Note that ^[ represents the ESCAPE character, which can be typed in by pressing CTRL-[ or CTRL-V followed by ESC.

I wrote this brief tutorial because I could not find instructions specific to this topic by searching Google. No doubt a better method exists to accomplish this colour-coding, but I’m not that bright.

  1. Ian Stevens

    May 3, 2005 at 4:14 pm

    Thanks, those are great for Apache or Linux server logs (eg. syslog, messages, etc), but overkill and heavyweight for logs output by my own server apps.

Leave a Reply