NAME

Apache::LogFormat::Compiler - Compile a log format string to perl-code

SYNOPSIS

use Apache::LogFormat::Compiler;

my $log_handler = Apache::LogFormat::Compiler->new("combined");
my $log = $log_handler->log_line(
    $env,
    $res,
    $length,
    $reqtime,
    $time
);

DESCRIPTION

Compile a log format string to perl-code. For faster generation of access_log lines.

METHOD

ADD CUSTOM FORMAT STRING

Apache::LogFormat::Compiler allows one to add a custom format string

my $log_handler = Apache::LogFormat::Compiler->new(
    '%z %{HTTP_X_FORWARDED_FOR|REMOTE_ADDR}Z',
    char_handlers => +{
        'z' => sub {
            my ($env,$req) = @_;
            return $env->{HTTP_X_FORWARDED_FOR};
        }
    },
    block_handlers => +{
        'Z' => sub {
            my ($block,$env,$req) = @_;
            # block eq 'HTTP_X_FORWARDED_FOR|REMOTE_ADDR'
            my ($main, $alt) = split('\|', $args);
            return exists $env->{$main} ? $env->{$main} : $env->{$alt};
        }
    },
);

Any single letter can be used, other than those already defined by Apache::LogFormat::Compiler. Your sub is called with two or three arguments: the content inside the {} from the format (block_handlers only), the PSGI environment ($env), and the ArrayRef of the response. It should return the string to be logged.

RECOMMENDED MODULE

If POSIX::strftime::GNU is available, Apache::LogFormat::Compiler uses it. It's good for Windows and old Unices have limited strftime's formatting.

AUTHOR

Masahiro Nagano kazeburo@gmail.com

SEE ALSO

Plack::Middleware::AccessLog, http://httpd.apache.org/docs/2.2/mod/mod_log_config.html

LICENSE

Copyright (C) Masahiro Nagano

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.