Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to color text in log files in linux

I want to add colors to the text in Log files. For e.g. I would want lines that contain text 'ERROR', to be red colored. So that when I view that file, these I should be able to easily find those lines with 'ERROR'. I tried looking for the answer to question but couldn't find anything helpful. Thanks in advnace.

like image 982
krish___na Avatar asked Mar 07 '18 10:03

krish___na


2 Answers

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.

like image 178
romainl Avatar answered Sep 28 '22 03:09

romainl


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...

like image 36
Zaboj Campula Avatar answered Sep 28 '22 04:09

Zaboj Campula