Colored output in less when tailing a logfile

colorslesslogssed

I am in dire need of a way to color my less output while reading a file that is constantly being appended.

The file in questions is a Resin servlet container log.

My current "implementation" of the color scheme works using tail -F and sed for editing in colors around keywords. I have four words to color: INFO, WARN, ERROR, DEBUG, one of which occurs once per line.

I have tried using the LESSOPENenvironment variable, but I can't seem to keep reading the log. The file is not tailed once it has been open.

I have very little control over the server, and cannot really install anything other than my own scripts in my home folder, so no packages. The server in question is a RHEL 6.4.

The problem is that less does not keep reading the file.
Is there a way to tail the log continuously using the LESSOPENenvironment variable, or do I need some more complex tools?

Best Answer

The problem lies in the pipes on linux. CTRLc closes the pipe and less cannot reopen the pipe.

The solution I found viable was to redirect the colored log to a file, then read that file with less. A file can be tailed after a CTRL-c, and I therefore just do the following:

tail -F -c +1 | colorize > /tmp/logfilename &
less -Sr /tmp/logfilename

Works like a charm.

Related Question