Apache::LogRegex - Parse a line from an Apache logfile into a hash
This document refers to version 1.5 of Apache::LogRegex, released November 20th 2008
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 } }
Designed as a simple class to parse Apache 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.
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.
Given a LINE from an Apache logfile it will parse the line and return a hash of all the elements of the line indexed by their format. If the line cannot be parsed an empty hash will be returned.
Returns a list of field names that were extracted from the data. Such as '%a', '%t' and '%r' from the above example.
Returns a copy of the regex that will be used to parse the log file.
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.
Perl 5
The only problem I can foresee is the various custom time formats but providing that they are encased in '[' and ']' all should be fine.
When the constructor is called it requires one argument. This message is given if more or less arguments were supplied.
The correct number of arguments were supplied with the constructor call, however the first argument, FORMAT, was undefined.
When the method is called it requires one argument. This message is given if more or less arguments were supplied.
The correct number of arguments were supplied with the method call, however the first argument, LINE, was undefined.
When the method is called it requires no arguments. This message is given if some arguments were supplied.
None so far
None
mod_log_config for a description of the Apache format commands
Peter Hickman (peterhi@ntlworld.com)
Copyright (c) 2004, Peter Hickman. All rights reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.
To install Apache::LogRegex, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Apache::LogRegex
CPAN shell
perl -MCPAN -e shell install Apache::LogRegex
For more information on module installation, please visit the detailed CPAN module installation guide.