linux less command and color logs

When you have a script to colour your logs from an application and its name is “colorize” you should place it in the PATH environment variable and the do the following:

Create:

$HOME/bin/lessopen.sh
#!/bin/sh
case "$1" in
*.log)
     tail -n+1 -f $1 | colorize > /tmp/less.$$ &
     echo $[$! - 1] > /tmp/tail_pid.$$
     echo /tmp/less.$$
    ;;
esac

Create:

$HOME/bin/lessclose.sh
#! /bin/sh
rm $2
old_pid=`echo $2 | sed 's/\/tmp\/less\.//'`
kill -9 `cat /tmp/tail_pid.$old_pid`
rm /tmp/tail_pid.$old_pid

Add to .bash_profile

export LESSOPEN="$HOME/bin/lessopen.sh %s"
export LESSCLOSE="$HOME/bin/lessclose.sh %s %s"

And the run less, with the working “+F” option:

less -R +F /some/file/server.log

Posted by wojtek Sun, 14 Dec 2008 13:58:00 GMT




Comments

Leave a response

Leave a comment