Apache::LogRegex(3)

πŸ“¦
version 1.71 Β· 2 versions
, in fedora 43

πŸ’– 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 a Apache::LogRegex object 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 the CustomLog string from your httpd.conf file. 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, or undef if 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>


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