in reply to Parsing Apache logs with Regex

So I found this LogRegex think here: http://search.cpan.org/~peterhi/Apache-LogRegex-1.5/lib/Apache/LogRegex.pm But does anyone know how to use it? The example code fails all over the place and doesn't do anything for my logfile line.

Replies are listed 'Best First'.
Re^2: Parsing Apache logs with Regex
by atcroft (Abbot) on Dec 31, 2008 at 22:21 UTC

    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.

Re^2: Parsing Apache logs with Regex
by Anonymous Monk on Dec 31, 2008 at 21:42 UTC
    What did you write, because it works for me!