Apache::LogRegex(3)
π NAME π
Apache::LogRegex β bestie, this thing takes a single line from an Apache logfile and turns it into a hash. That's the whole vibe. β¨
πΈ VERSION πΈ
version 1.71
π 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
}
π¦ DESCRIPTION π¦
β¨ Overview β¨
okay so. This is a simple little class for parsing Apache access log files. You hand it a log file format, it builds a regex that matches that format, and then it chews through your logfile line by line and hands you back a hash for each line. It's giving glow-up for your log parsing. π
The field names in that hash come straight from the log file format. So if your format is '%a %t \"%r\" %s %b %T \"%{Referer}i\" ...' then the keys of the hash are gonna be %a, %t, %r, %s, %b, %T and %{Referer}i. Literally just the format tokens, periodt.
Should those key names be unusable β and yeah, I'd guess they probably are, ngl β then subclass it and provide an override rename_this_name() method that renames the keys before they get added into the array of field names. Slay. π«Ά
This module supports variable spacing between elements that are surrounded by quotes, so if you've got more than one space between those elements in your format or in your log file, that should be totally fine. No stress. π
π SUBROUTINES/METHODS π
π Constructor π
Apache::LogRegex->new( FORMAT )β Returns aApache::LogRegexobject that can parse a line from an Apache logfile that was written to with the FORMAT string. And bestie, what is that FORMAT string? It's theCustomLogstring from yourhttpd.conffile. That one. π―
β Class and object methods β
parse( LINE )β Give it a LINE from an Apache logfile and it parses that line and returns all the elements of the line, indexed by their corresponding format string. In scalar context you get a hash reference. In list context you get a flat paired list. And in either context, if the line can't be parsed, you get a false value back. No cap. πgenerate_parser( LIST )β Generates and returns a closure. When you call that closure with a line, it returns a hash reference containing the parsed fields, orundefif the parse failed. If LIST is supplied, it's interpreted as a flattened hash of arguments. One argument is recognised: if"reuse_record"is a true value, then the closure reuses the same hash reference every single time it's called. The default is to allocate a brand new hash for each result. β¨
Calling this closure is significantly faster than the parse method. Like, actually. π₯
names()β Returns a list of field names that were extracted from the data. Such as'%a','%t'and'%r'from the example above. Cute, right? πregex()β Returns a copy of the regex that will be used to parse the log file. Peek behind the curtain, iykyk. π§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. Override it in a subclass, bestie. π
π CONFIGURATION AND ENVIRONMENT π
Perl 5
π DIAGNOSTICS π
The various custom time formats could be problematic β but providing that they're encased in '[' and ']' all should be fine. π«Ά
Apache::LogRegex->new() takes 1 argumentβ When the constructor is called it requires one argument. This message shows up if more or less arguments were supplied.Apache::LogRegex->new() argument 1 (FORMAT) is undefinedβ The correct number of arguments were supplied with the constructor call, however the first argument, FORMAT, was undefined.Apache::LogRegex->parse() takes 1 argumentβ When the method is called it requires one argument. This message shows up if more or less arguments were supplied.Apache::LogRegex->parse() argument 1 (LINE) is undefinedβ The correct number of arguments were supplied with the method call, however the first argument, LINE, was undefined.Apache::LogRegex->names() takes no argumentβ When the method is called it requires no arguments. This message shows up if some arguments were supplied.Apache::LogRegex->regex() takes no argumentβ When the method is called it requires no arguments. This message shows up if some arguments were supplied.
π BUGS π
None so far
π FILES π
None
π§ SEE ALSO π§
mod_log_config for a description of the Apache format commands
π THANKS π
Peter Hickman wrote the original module and maintained it for several years. He kindly passed maintainership on just prior to the 1.51 release. Most of the features of this module are the fruits of his work. If you find any bugs they are my doing.
π AUTHOR π
Original code by Peter Hickman <peterhi@ntlworld.com>
Additional code by Andrew Kirkpatrick <ubermonk@gmail.com>
πΈ LICENSE AND COPYRIGHT πΈ
Original code copyright (c) 2004-2006 Peter Hickman. All rights reserved.
Additional code copyright (c) 2013 Andrew Kirkpatrick. All rights reserved.
This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.
and that's the whole page, bestie β this one's from perl v5.42.0, dated 2025-07-25, and it lives at Apache::LogRegex(3). go forth and parse, mwah πβ¨
π¦ where this page comes from
- fedora 43: perl-Apache-LogRegex 1.71-28.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms