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 version 1.71
10
12 use Apache::LogRegex;
13
14 my $lr;
15
16 eval { $lr = Apache::LogRegex->new($log_format) };
17 die "Unable to parse log line: $@" if ($@);
18
19 my %data;
20
21 while ( my $line_from_logfile = <> ) {
22 eval { %data = $lr->parse($line_from_logfile); };
23 if (%data) {
24 # We have data to process
25 } else {
26 # We could not parse this line
27 }
28 }
29
30 # or generate a closure for better performance
31
32 my $parser = $lr->generate_parser;
33
34 while ( my $line_from_logfile = <> ) {
35 my $data = $parser->($line_from_logfile) or last;
36 # We have data to process
37 }
38
40 Overview
41 A simple class to parse Apache access log files. It will construct a
42 regex that will parse the given log file format and can then parse
43 lines from the log file line by line returning a hash of each line.
44
45 The field names of the hash are derived from the log file format. Thus
46 if the format is '%a %t \"%r\" %s %b %T \"%{Referer}i\" ...' then the
47 keys of the hash will be %a, %t, %r, %s, %b, %T and %{Referer}i.
48
49 Should these key names be unusable, as I guess they probably are, then
50 subclass and provide an override rename_this_name() method that can
51 rename the keys before they are added in the array of field names.
52
53 This module supports variable spacing between elements that are
54 surrounded by quotes, so if you have more than one space between those
55 elements in your format or in your log file, that should be OK.
56
58 Constructor
59 Apache::LogRegex->new( FORMAT )
60 Returns a Apache::LogRegex object that can parse a line from an
61 Apache logfile that was written to with the FORMAT string. The
62 FORMAT string is the CustomLog string from the httpd.conf file.
63
64 Class and object methods
65 parse( LINE )
66 Given a LINE from an Apache logfile it will parse the line and
67 return all the elements of the line indexed by their corresponding
68 format string. In scalar context this takes the form of a hash
69 reference, in list context a flat paired list. In either context,
70 if the line cannot be parsed a false value will be returned.
71
72 generate_parser( LIST )
73 Generate and return a closure that, when called with a line, will
74 return a hash reference containing the parsed fields, or undef if
75 the parse failed. If LIST is supplied, it is interpreted as a
76 flattened hash of arguments. One argument is recognised; if
77 "reuse_record" is a true value, then the closure will reuse the
78 same hash reference each time it is called. The default is to
79 allocate a new hash for each result.
80
81 Calling this closure is significantly faster than the "parse"
82 method.
83
84 names()
85 Returns a list of field names that were extracted from the data.
86 Such as '%a', '%t' and '%r' from the above example.
87
88 regex()
89 Returns a copy of the regex that will be used to parse the log
90 file.
91
92 rename_this_name( NAME )
93 Use this method to rename the keys that will be used in the
94 returned hash. The initial NAME is passed in and the method should
95 return the new name.
96
98 Perl 5
99
101 The various custom time formats could be problematic but providing that
102 they are encased in '[' and ']' all should be fine.
103
104 Apache::LogRegex->new() takes 1 argument
105 When the constructor is called it requires one argument. This
106 message is given if more or less arguments were supplied.
107
108 Apache::LogRegex->new() argument 1 (FORMAT) is undefined
109 The correct number of arguments were supplied with the constructor
110 call, however the first argument, FORMAT, was undefined.
111
112 Apache::LogRegex->parse() takes 1 argument
113 When the method is called it requires one argument. This message is
114 given if more or less arguments were supplied.
115
116 Apache::LogRegex->parse() argument 1 (LINE) is undefined
117 The correct number of arguments were supplied with the method call,
118 however the first argument, LINE, was undefined.
119
120 Apache::LogRegex->names() takes no argument
121 When the method is called it requires no arguments. This message is
122 given if some arguments were supplied.
123
124 Apache::LogRegex->regex() takes no argument
125 When the method is called it requires no arguments. This message is
126 given if some arguments were supplied.
127
129 None so far
130
132 None
133
135 mod_log_config for a description of the Apache format commands
136
138 Peter Hickman wrote the original module and maintained it for several
139 years. He kindly passed maintainership on just prior to the 1.51
140 release. Most of the features of this module are the fruits of his
141 work. If you find any bugs they are my doing.
142
144 Original code by Peter Hickman <peterhi@ntlworld.com>
145
146 Additional code by Andrew Kirkpatrick <ubermonk@gmail.com>
147
149 Original code copyright (c) 2004-2006 Peter Hickman. All rights
150 reserved.
151
152 Additional code copyright (c) 2013 Andrew Kirkpatrick. All rights
153 reserved.
154
155 This module is free software. It may be used, redistributed and/or
156 modified under the same terms as Perl itself.
157
158
159
160perl v5.36.0 2023-01-19 Apache::LogRegex(3)