Apache logfile colorizer

This simple script reads loglines in Apaches “combined” format from STDIN, and prints relevant data nicely colored to the terminal. This script is very nice to watch whats going on on your website.

#!/usr/bin/perl
 
$black="\033[30m";
$red="\033[31m";
$green="\033[32m";
$yellow="\033[33m";
$blue="\033[34m";
$magenta="\033[35m";
$cyan="\033[36m";
$white="\033[37m";
 
while(<STDIN>){
 m/(\S+?) (\S+?) (\S+?) \[(.+?)\] "\S+? (\S+?) HTTP\S+?" (\d*?) (\d*?) "(.*?)" "(.*?)"/;
 
 print $red;
 print "$1 ";     # IP
 print $green;
 print "$6 ";     # Status
 print $yellow;
 print "$5 ";     # Request
 print $cyan;
 print "$8 ";     # Referrer
 print $magenta;
 print "$9 ";     # Browser
 print $white;
 print "\n";
}

Example Usage:

$> tail -f /var/log/apache/access.log | ./livelog.pl