5

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?

Mat
  • 52,586
Jes
  • 461
  • Is the file already coloured using ANSI escape sequences or do you need to add the colours as well? – Marco Mar 27 '13 at 12:49
  • You haven't stated your exact problem. What is not working? Is less not getting new output, is it stripping the colors, what? – phemmer Mar 27 '13 at 13:06
  • Colors are working, less is not tailing, it just reads what the file looks like when it's first opened. – Jes Mar 27 '13 at 13:09
  • 1
    have a look at multitail – Ulrich Dangel Mar 27 '13 at 13:33
  • Shift-f does not tail it. multitail is not an option, can't install anything on the server. – Jes Mar 27 '13 at 14:01
  • @Jes try pressing SHIFT+G when in less to have it grab new data and jump to the end – phemmer Mar 27 '13 at 14:31
  • The problem is that CTRL+C closes the pipe. The solution is to use a temporary file to tail the colored log into, then less that file. Found a similar question: http://superuser.com/questions/186793/less-tail-mode-pipes – Jes Apr 02 '13 at 11:00
  • Who pressed ctrl+c? That is not in the question. – ctrl-alt-delor Nov 29 '13 at 10:25

4 Answers4

3

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.

Jes
  • 461
1

I see two questions here:

  • How do I color output in less
  • How do I make less behave like tail -f

I can't answer the first one, though LESSOPEN seems like a good avenue. The second one is easy: start less as less +F or hit ShiftF if less is already running.

1

You could run tail -f in an Emacs shell buffer, and let Emacs do the coloring.

Hi Lock lets you quickly highlight words matching a regexp. There's a crude way to save highlighting patterns in a file, but it won't be convenient when the file is actually the output of a command. You're probably better off writing a simple major mode with font lock keywords.

0

I use vim to simulate colored less. It has a less macro and uses the Vim color scheme.

I added this to my bashrc:

alias cless='/usr/share/vim/vim73/macros/less.sh'

For the tailing/following, you will need some Vim plugin that allows for following. Perhaps try the Tail Bundle plugin.

smcg
  • 463