1Apache::LogRegex(3) User Contributed Perl Documentation Apache::LogRegex(3)
2
3
4
6 Apache::LogRegex - Parse a line from an Apache logfile into a hash
7
9 This document refers to version 1.5 of Apache::LogRegex, released
10 November 20th 2008
11
13 use Apache::LogRegex;
14
15 my $lr;
16
17 eval { $lr = Apache::LogRegex->new($log_format) };
18 die "Unable to parse log line: $@" if ($@);
19
20 my %data;
21
22 while ( my $line_from_logfile = <> ) {
23 eval { %data = $lr->parse($line_from_logfile); };
24 if (%data) {
25 # We have data to process
26 } else {
27 # We could not parse this line
28 }
29 }
30
32 Overview
33 Designed as a simple class to parse Apache log files. It will construct
34 a regex that will parse the given log file format and can then parse
35 lines from the log file line by line returning a hash of each line.
36
37 The field names of the hash are derived from the log file format. Thus
38 if the format is '%a %t \"%r\" %s %b %T \"%{Referer}i\" ...' then the
39 keys of the hash will be %a, %t, %r, %s, %b, %T and %{Referer}i.
40
41 Should these key names be unusable, as I guess they probably are, then
42 subclass and provide an override rename_this_name() method that can
43 rename the keys before they are added in the array of field names.
44
45 Constructors and initialization
46 Apache::LogRegex->new( FORMAT )
47 Returns a Apache::LogRegex object that can parse a line from an
48 Apache logfile that was written to with the FORMAT string. The
49 FORMAT string is the CustomLog string from the httpd.conf file.
50
51 Class and object methods
52 parse( LINE )
53 Given a LINE from an Apache logfile it will parse the line and
54 return a hash of all the elements of the line indexed by their
55 format. If the line cannot be parsed an empty hash will be
56 returned.
57
58 names()
59 Returns a list of field names that were extracted from the data.
60 Such as '%a', '%t' and '%r' from the above example.
61
62 regex()
63 Returns a copy of the regex that will be used to parse the log
64 file.
65
66 rename_this_name( NAME )
67 Use this method to rename the keys that will be used in the
68 returned hash. The initial NAME is passed in and the method should
69 return the new name.
70
72 Perl 5
73
75 The only problem I can foresee is the various custom time formats but
76 providing that they are encased in '[' and ']' all should be fine.
77
78 Apache::LogRegex->new() takes 1 argument
79 When the constructor is called it requires one argument. This
80 message is given if more or less arguments were supplied.
81
82 Apache::LogRegex->new() argument 1 (FORMAT) is undefined
83 The correct number of arguments were supplied with the constructor
84 call, however the first argument, FORMAT, was undefined.
85
86 Apache::LogRegex->parse() takes 1 argument
87 When the method is called it requires one argument. This message is
88 given if more or less arguments were supplied.
89
90 Apache::LogRegex->parse() argument 1 (LINE) is undefined
91 The correct number of arguments were supplied with the method call,
92 however the first argument, LINE, was undefined.
93
94 Apache::LogRegex->names() takes no argument
95 When the method is called it requires no arguments. This message is
96 given if some arguments were supplied.
97
98 Apache::LogRegex->regex() takes no argument
99 When the method is called it requires no arguments. This message is
100 given if some arguments were supplied.
101
103 None so far
104
106 None
107
109 mod_log_config for a description of the Apache format commands
110
112 Peter Hickman (peterhi@ntlworld.com)
113
115 Copyright (c) 2004, Peter Hickman. All rights reserved. This module is
116 free software. It may be used, redistributed and/or modified under the
117 same terms as Perl itself.
118
119
120
121perl v5.12.0 2008-11-20 Apache::LogRegex(3)