How to color text in log files in linux How to color text in log files in linux unix unix

How to color text in log files in linux


So that when I view that file, these I should be able to easily find those lines with 'ERROR'.

Coloring those lines would be one way but there's a much simpler and more idiomatic way:

$ grep ERROR /path/to/logfile | less

will show you every line containing ERROR from /path/to/logfile in less.


Someprogrammerdude suggested to use ability of viewers to colorize output. It is called 'syntax highlighting' in vim ecosystem but not only there.

The simplest thing you can do in vim is:

:sy match my_error /.*ERROR.*/:hi my_error ctermfg=red guifg=red

You can add these lines to your .vimrc or may better is to create a special syntax file for your log files where you can define more rules...


I wrote a utility for coloring log files called TxtStyle. It can color log files based on regex patterns defined in a config file ~/.txts.conf:

[Style="example"]!red: regex("error")green: regex("\d{4}-\d\d-\d\d")# .. snip ..

To try it out, run (requires Python):

sudo pip install TxtStylewget -q https://raw.githubusercontent.com/armandino/TxtStyle/master/example.logtxts -n example example.log

enter image description here