Categories
Tech

tail and format Apache log files (Perl one-liner)

Here is a quick Perl one-liner that lets you tail an Apache log file and view nicely-formatted output:

tail -f YOURDOMAIN_LOG_FILE | perl -ne '$|=1;my ($host,$date,$url,$status,$size,$referrer,$agent) = $_ =~ m/^(\S+) - - \[(\S+ [\-|\+]\d{4})\] "(\S+ \S+ [^"]+)" (\d{3}) (\d+|-) "(.*?)" "([^"]+)"$/;print $host . " - " . $date . " - " . $url ."\n";'

As this is a “one-liner”, it needs to all be pasted on one line of course, and you need to replace “YOURDOMAIN_LOG_FILE” with the actual file you want to tail. This might be in /etc/httpd/domlogs/NAME_OF_FILE.

Output looks like this:


nn.nn.nn.nn - 16/Jul/2010:07:59:56 -0700 - GET /favicon.ico HTTP/1.1

It fails with authenticated users, but it’s fine for a quick view of logs for a site that doesn’t use basic_auth.

One reply on “tail and format Apache log files (Perl one-liner)”

Thanks for the great one-liner. To handle the authenticated users lines successfully you may substitute the user columns (the second “-” in the regex) using a non-capturing paren grouping like
(?:-|\S+)

Cheers,
kevin

Leave a Reply

Your email address will not be published. Required fields are marked *