in reply to Re: Parsing Apache logs with Regex
in thread Parsing Apache logs with Regex

I adapted the example given only slightly and had no real difficulty (other than you have to give it the Apache log format string to use):

#!/usr/bin/perl use strict; use warnings; use Apache::LogRegex; use Data::Dumper; my $lr; my $log_format = q/%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"/; eval { $lr = Apache::LogRegex->new($log_format) }; die "Unable to parse log line: $@" if ($@); my %data; open DF, $ARGV[0] or die $!; while ( my $line_from_logfile = <DF> ) { eval { %data = $lr->parse($line_from_logfile); }; if (%data) { print Data::Dumper->Dump( [ \$line_from_logfile, \%data ], [qw(*line_from_logfile *data)] ), qq{\n}; # We have data to process } else { # We could not parse this line } } close DF;

With this, I got the following result (using Data::Dumper for output):

$line_from_logfile = \'192.168.1.100 - - [07/Dec/2008:04:24:39 -0600] +"GET /some/file/here.html HTTP/1.1" 304 - "http://www.some-referring- +webserver.com/some/other/page.html" "Mozilla/4.0 (compatible; MSIE 7. +0; Windows NT 5.1; .NET CLR 1.1.4322)" '; %data = ( '%{Referer}i' => 'http://www.some-referring-webserver.com/ +some/other/page.html', '%{User-Agent}i' => 'Mozilla/4.0 (compatible; MSIE 7.0; Wi +ndows NT 5.1; .NET CLR 1.1.4322)', '%t' => '[07/Dec/2008:04:24:39 -0600]', '%r' => 'GET /some/file/here.html HTTP/1.1', '%h' => '192.168.1.100', '%b' => '-', '%l' => '-', '%u' => '-', '%>s' => '304' );

Hope that helps.