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?

8
  • Is the file already coloured using ANSI escape sequences or do you need to add the colours as well?
    – Marco
    Commented Mar 27, 2013 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
    Commented Mar 27, 2013 at 13:06
  • Colors are working, less is not tailing, it just reads what the file looks like when it's first opened.
    – Jes
    Commented Mar 27, 2013 at 13:09
  • 1
    have a look at multitail Commented Mar 27, 2013 at 13:33
  • Shift-f does not tail it. multitail is not an option, can't install anything on the server.
    – Jes
    Commented Mar 27, 2013 at 14:01

4 Answers 4

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.

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .