Apache-LogRegex

 view release on metacpan or  search on metacpan

lib/Apache/LogRegex.pm  view on Meta::CPAN

    return $self->{_regex};
}

sub rename_this_name {
    my ($self, $name) = @_;

    return $name;
}

1;

=head1 NAME

Apache::LogRegex - Parse a line from an Apache logfile into a hash

=head1 VERSION

version 1.71

=head1 SYNOPSIS

  use Apache::LogRegex;

  my $lr;

  eval { $lr = Apache::LogRegex->new($log_format) };
  die "Unable to parse log line: $@" if ($@);

  my %data;

  while ( my $line_from_logfile = <> ) {
      eval { %data = $lr->parse($line_from_logfile); };
      if (%data) {
          # We have data to process
      } else {
          # We could not parse this line
      }
  }

  # or generate a closure for better performance

  my $parser = $lr->generate_parser;

  while ( my $line_from_logfile = <> ) {
      my $data = $parser->($line_from_logfile) or last;
      # We have data to process
  }

=head1 DESCRIPTION

=head2 Overview

A simple class to parse Apache access log files. It will construct a
regex that will parse the given log file format and can then parse
lines from the log file line by line returning a hash of each line.

The field names of the hash are derived from the log file format. Thus if
the format is '%a %t \"%r\" %s %b %T \"%{Referer}i\" ...' then the keys of
the hash will be %a, %t, %r, %s, %b, %T and %{Referer}i.

Should these key names be unusable, as I guess they probably are, then subclass
and provide an override rename_this_name() method that can rename the keys
before they are added in the array of field names.

This module supports variable spacing between elements that are
surrounded by quotes, so if you have more than one space between those
elements in your format or in your log file, that should be OK.

=head1 SUBROUTINES/METHODS

=head2 Constructor

=over 4

=item Apache::LogRegex->new( FORMAT )

Returns a Apache::LogRegex object that can parse a line from an Apache
logfile that was written to with the FORMAT string. The FORMAT
string is the CustomLog string from the httpd.conf file.

=back

=head2 Class and object methods

=over 4

=item parse( LINE )

Given a LINE from an Apache logfile it will parse the line and return
all the elements of the line indexed by their corresponding format
string. In scalar context this takes the form of a hash reference, in
list context a flat paired list. In either context, if the line cannot
be parsed a false value will be returned.

=item generate_parser( LIST )

Generate and return a closure that, when called with a line, will
return a hash reference containing the parsed fields, or undef if the
parse failed. If LIST is supplied, it is interpreted as a flattened
hash of arguments. One argument is recognised; if C<reuse_record> is a
true value, then the closure will reuse the same hash reference each
time it is called. The default is to allocate a new hash for each
result.

Calling this closure is significantly faster than the C<parse> method.

=item names()

Returns a list of field names that were extracted from the data. Such as
'%a', '%t' and '%r' from the above example.

=item regex()

Returns a copy of the regex that will be used to parse the log file.

=item rename_this_name( NAME )

Use this method to rename the keys that will be used in the returned hash.
The initial NAME is passed in and the method should return the new name.

=back



( run in 2.668 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )