blib/lib/Apache/LogRegex.pm

filter: Statementstmt 96.3% 80 / 83 Branchbran 92.1% 35 / 38 Conditioncond 88.8% 8 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 13 / 13 Podpod 85.7% 6 / 7 totaltotal 92.9% 144 / 155 CC 23 SCAR 17.7
CC
23
Cov
95%
_parse_format
12.1
parse
5.0
new
3.0
CRAP 5.8
↑ Summary
1 package Apache::LogRegex;
2 1 $Apache::LogRegex::VERSION = '1.71';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
7 5 my ($class, $format) = @_;
8  
9 5 ▶ die __PACKAGE__ . '->new() takes 1 argument' unless @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 2 3 5
10 3 ▶ die __PACKAGE__ . '->new() argument 1 (FORMAT) is false' unless $format;
Branch: 1 branch
BranchTrueFalseTotal
unless $format 2 1 3
11  
12 1 my $self = bless {}, $class;
13  
14 1 $self->{_format} = $format;
15  
16 1 $self->{_regex} = '';
17 1 $self->{_regex_fields} = undef;
18  
19 1 $self->_parse_format();
20  
21 1 return $self;
22 }
23  
24 1 ▶sub _parse_format {
Subroutine _parse_format: called CC=12 SCAR=24.9
25 1 my ($self) = @_;
26  
27 11 ▶ sub quoted_p {
Subroutine quoted_p: called CC=1 SCAR=0.0
Pod undocumented
28 11 $_[0] =~ m/^\\\"/;
29 }
30  
31 1 chomp $self->{_format};
32 1 $self->{_format} =~ s#[ \t]+# #;
33 1 $self->{_format} =~ s#^ ##;
34 1 $self->{_format} =~ s# $##;
35  
36 1 my @format_elements = split /\s+/, $self->{_format};
37 1 my $regex_string = '';
38  
39 1 ▶ for (my $i = 0; $i < @format_elements; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @format_elements) 9 1 10
40 9 my $element = $format_elements[$i];
41 9 my $quoted = quoted_p($element);
42  
43 9 ▶ if ($quoted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quoted) 3 6 9
44 3 $element =~ s/^\\\"//;
45 3 $element =~ s/\\\"$//;
46 }
47  
48 9 push @{ $self->{_regex_fields} }, $self->rename_this_name($element);
49  
50 9 my $group = '(\S*)';
51  
52 9 ▶ if ($quoted) {
Branch: 3 branches
BranchTrueFalseTotal
if ($quoted) { } 3 6 9
elsif ($element =~ /^%.*t$/) { } 1 5 6
elsif ($element eq '%U') { } 0 5 5
53 3 ▶ if ($element eq '%r' or $element =~ m/{Referer}/ or $element =~ m/{User-Agent}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element eq '%r' or $element =~ /{Referer}/ or $element =~ /{User-Agent}/) { } 3 0 3
Condition83%5/6
$element eq "%r" or $element =~ /{Referer}/ or $element =~ /{User-Agent}/
l!l&&r!l&&!r
210
$element eq "%r" or $element =~ /{Referer}/
l!l&&r!l&&!r
111
MC/DC0%0/3
$element eq "%r" or $element =~ /{Referer}/ or $element =~ /{User-Agent}/
$element eq "%r" $element =~ /{Referer}/ $element =~ /{User-Agent}/
Truth table75%3/4
A or B or C
A= $element eq "%r"B= $element =~ /{Referer}/C= $element =~ /{User-Agent}/
ABCresult
1XX1
01X1
0011
0000
54 3 $group = qr/"([^"\\]*(?:\\.[^"\\]*)*)"/;
55 }
56 else {
57 0 $group = '\"([^\"]*)\"';
58 }
59 }
60 elsif ($element =~ m/^%.*t$/) {
61 1 $group = '(\[[^\]]+\])';
62 }
63 elsif ($element eq '%U') {
64 0 $group = '(.+?)';
65 }
66  
67 9 $regex_string .= $group;
68  
69 # expect elements separated by whitespace
70 9 ▶ if ($i < $#format_elements) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#format_elements) 8 1 9
71 8 my $next_element = $format_elements[$i + 1];
72 8 ▶ if ($quoted && quoted_p($next_element)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quoted and quoted_p($next_element)) { } 1 7 8
Condition100%3/3
$quoted and quoted_p($next_element)
!ll&&!rl&&r
611
MC/DC100%2/2
$quoted and quoted_p($next_element)
$quoted quoted_p($next_element)
Truth table100%3/3
A and B
A= $quotedB= quoted_p($next_element)
ABresult
0X0
100
111
73 # tolerate multiple whitespaces iff both elements are quoted
74 1 $regex_string .= '\s+';
75 } else {
76 7 $regex_string .= '\s';
77 }
78 }
79 }
80  
81 1 $self->{_regex} = qr/^$regex_string\s*$/;
82 }
83  
84 10 ▶sub parse {
Subroutine parse: called CC=5 SCAR=16.1
Pod documented
85 10 my ($self, $line) = @_;
86  
87 10 ▶ die __PACKAGE__ . '->parse() takes 1 argument' unless @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 2 8 10
88 8 ▶ die __PACKAGE__ . '->parse() argument 1 (LINE) is undefined' unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 7 8
89  
90 7 ▶ if (my @temp = $line =~ $self->{_regex}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@temp) = $line =~ /$self->{"_regex"}/) 5 2 7
91 5 my %data;
92 5 @data{ @{ $self->{_regex_fields} } } = @temp;
93 5 ▶ return wantarray ? %data : \%data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 1 5
94 }
95  
96 2 return;
97 }
98  
99 2 ▶sub generate_parser {
Subroutine generate_parser: called CC=2 SCAR=6.9
Pod documented
100 2 my ($self, %args) = @_;
101  
102 2 my $regex = $self->{_regex};
103 2 my @fields = @{ $self->{_regex_fields} };
104  
105 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
106  
107 2 ▶ if ($args{reuse_record}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'reuse_record'}) { } 1 1 2
108 1 my $record = {};
109 7 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
110 7 ▶ if (@$record{@fields} = $_[0] =~ $regex) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$record{@fields} = $_[0] =~ /$regex/) { } 3 4 7
111 3 return $record;
112 } else {
113 4 return;
114 }
115 }
116 1 } else {
117 2 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
118 2 my $record = {};
119 2 ▶ if (@$record{@fields} = $_[0] =~ $regex) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$record{@fields} = $_[0] =~ /$regex/) { } 2 0 2
120 2 return $record;
121 } else {
122 0 return;
123 }
124 }
125 1 }
126 }
127  
128 2 ▶sub names {
Subroutine names: called CC=2 SCAR=6.9
Pod documented
129 2 my ($self) = @_;
130  
131 2 ▶ die __PACKAGE__ . '->names() takes no argument' unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 1 1 2
132  
133 1 return @{ $self->{_regex_fields} };
134 }
135  
136 2 ▶sub regex {
Subroutine regex: called CC=2 SCAR=6.9
Pod documented
137 2 my ($self) = @_;
138  
139 2 ▶ die __PACKAGE__ . '->regex() takes no argument' unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 1 1 2
140  
141 1 return $self->{_regex};
142 }
143  
144 9 ▶sub rename_this_name {
Subroutine rename_this_name: called CC=1 SCAR=0.0
Pod documented
145 9 my ($self, $name) = @_;
146  
147 9 return $name;
148 }
149  
150 1 1;
151  
152 =head1 NAME
153  
154 Apache::LogRegex - Parse a line from an Apache logfile into a hash
155  
156 =head1 VERSION
157  
158 version 1.71
159  
160 =head1 SYNOPSIS
161  
162 use Apache::LogRegex;
163  
164 my $lr;
165  
166 eval { $lr = Apache::LogRegex->new($log_format) };
167 die "Unable to parse log line: $@" if ($@);
168  
169 my %data;
170  
171 while ( my $line_from_logfile = <> ) {
172 eval { %data = $lr->parse($line_from_logfile); };
173 if (%data) {
174 # We have data to process
175 } else {
176 # We could not parse this line
177 }
178 }
179  
180 # or generate a closure for better performance
181  
182 my $parser = $lr->generate_parser;
183  
184 while ( my $line_from_logfile = <> ) {
185 my $data = $parser->($line_from_logfile) or last;
186 # We have data to process
187 }
188  
189 =head1 DESCRIPTION
190  
191 =head2 Overview
192  
193 A simple class to parse Apache access log files. It will construct a
194 regex that will parse the given log file format and can then parse
195 lines from the log file line by line returning a hash of each line.
196  
197 The field names of the hash are derived from the log file format. Thus if
198 the format is '%a %t \"%r\" %s %b %T \"%{Referer}i\" ...' then the keys of
199 the hash will be %a, %t, %r, %s, %b, %T and %{Referer}i.
200  
201 Should these key names be unusable, as I guess they probably are, then subclass
202 and provide an override rename_this_name() method that can rename the keys
203 before they are added in the array of field names.
204  
205 This module supports variable spacing between elements that are
206 surrounded by quotes, so if you have more than one space between those
207 elements in your format or in your log file, that should be OK.
208  
209 =head1 SUBROUTINES/METHODS
210  
211 =head2 Constructor
212  
213 =over 4
214  
215 =item Apache::LogRegex->new( FORMAT )
216  
217 Returns a Apache::LogRegex object that can parse a line from an Apache
218 logfile that was written to with the FORMAT string. The FORMAT
219 string is the CustomLog string from the httpd.conf file.
220  
221 =back
222  
223 =head2 Class and object methods
224  
225 =over 4
226  
227 =item parse( LINE )
228  
229 Given a LINE from an Apache logfile it will parse the line and return
230 all the elements of the line indexed by their corresponding format
231 string. In scalar context this takes the form of a hash reference, in
232 list context a flat paired list. In either context, if the line cannot
233 be parsed a false value will be returned.
234  
235 =item generate_parser( LIST )
236  
237 Generate and return a closure that, when called with a line, will
238 return a hash reference containing the parsed fields, or undef if the
239 parse failed. If LIST is supplied, it is interpreted as a flattened
240 hash of arguments. One argument is recognised; if C<reuse_record> is a
241 true value, then the closure will reuse the same hash reference each
242 time it is called. The default is to allocate a new hash for each
243 result.
244  
245 Calling this closure is significantly faster than the C<parse> method.
246  
247 =item names()
248  
249 Returns a list of field names that were extracted from the data. Such as
250 '%a', '%t' and '%r' from the above example.
251  
252 =item regex()
253  
254 Returns a copy of the regex that will be used to parse the log file.
255  
256 =item rename_this_name( NAME )
257  
258 Use this method to rename the keys that will be used in the returned hash.
259 The initial NAME is passed in and the method should return the new name.
260  
261 =back
262  
263 =head1 CONFIGURATION AND ENVIRONMENT
264  
265 Perl 5
266  
267 =head1 DIAGNOSTICS
268  
269 The various custom time formats could be problematic but providing that
270 they are encased in '[' and ']' all should be fine.
271  
272 =over 4
273  
274 =item Apache::LogRegex->new() takes 1 argument
275  
276 When the constructor is called it requires one argument. This message is
277 given if more or less arguments were supplied.
278  
279 =item Apache::LogRegex->new() argument 1 (FORMAT) is undefined
280  
281 The correct number of arguments were supplied with the constructor call,
282 however the first argument, FORMAT, was undefined.
283  
284 =item Apache::LogRegex->parse() takes 1 argument
285  
286 When the method is called it requires one argument. This message is
287 given if more or less arguments were supplied.
288  
289 =item Apache::LogRegex->parse() argument 1 (LINE) is undefined
290  
291 The correct number of arguments were supplied with the method call,
292 however the first argument, LINE, was undefined.
293  
294 =item Apache::LogRegex->names() takes no argument
295  
296 When the method is called it requires no arguments. This message is
297 given if some arguments were supplied.
298  
299 =item Apache::LogRegex->regex() takes no argument
300  
301 When the method is called it requires no arguments. This message is
302 given if some arguments were supplied.
303  
304 =back
305  
306 =head1 BUGS
307  
308 None so far
309  
310 =head1 FILES
311  
312 None
313  
314 =head1 SEE ALSO
315  
316 mod_log_config for a description of the Apache format commands
317  
318 =head1 THANKS
319  
320 Peter Hickman wrote the original module and maintained it for
321 several years. He kindly passed maintainership on just prior to
322 the 1.51 release. Most of the features of this module are the
323 fruits of his work. If you find any bugs they are my doing.
324  
325 =head1 AUTHOR
326  
327 Original code by Peter Hickman <peterhi@ntlworld.com>
328  
329 Additional code by Andrew Kirkpatrick <ubermonk@gmail.com>
330  
331 =head1 LICENSE AND COPYRIGHT
332  
333 Original code copyright (c) 2004-2006 Peter Hickman. All rights reserved.
334  
335 Additional code copyright (c) 2013 Andrew Kirkpatrick. All rights reserved.
336  
337 This module is free software. It may be used, redistributed and/or
338 modified under the same terms as Perl itself.
339  
340 =cut
↑ Summary