I uploaded Apache::LogFormat::Compiler to CPAN.
cpan: https://metacpan.org/release/Apache-LogFormat-Compiler
github: https://github.com/kazeburo/Apache-LogFormat-Compiler
Apache::LogFormat::Compiler compiles log_format line ‘combined’, ‘common’ or ‘%h %l %u %t “%r” %>s %b’ to perl-code.
It’s faster than Plack::Middleware::AccessLog’s way that parses log_format by regexp each request.
my $log_handler = Apache::LogFormat::Compiler->new();
my $compile_log_app = builder {
enable sub {
my $app = shift;
sub {
my $env = shift;
my $res = $app->();
$env->{psgi.errors}->print($log_handler->log_line($env,$res,6,0));
}
};
sub{ [ 200, [], [ "Hello "] ] };
};
This sample psgiapp is 8 times faster than the app that uses PM::AcccessLog.
I have plan to use Apache::LogFormat::Compiler in Plack::Middleware::AxsLog for customize any log_format.
generated sample perl-code.
% perl -Ilib -ML -E 'say Apache::LogFormat::Compiler->new()->{log_handler_code} '
sub {
my ($env,$res,$length,$time) = @_;
my @lt = localtime;
my $t = sprintf '%02d/%s/%04d:%02d:%02d:%02d %s', $lt[3], $abbr[$lt[4]], $lt[5]+1900,
$lt[2], $lt[1], $lt[0], $tz;
q!! . ($env->{REMOTE_ADDR} || '-')
. q! ! . '-'
. q! ! . ($env->{REMOTE_USER} || '-')
. q! ! . "[" . $t . "]"
. q! "! . _safe($env->{REQUEST_METHOD}) . " " . _safe($env->{REQUEST_URI}) .
" " . $env->{SERVER_PROTOCOL}
. q!" ! . $res->[0]
. q! ! . (defined $length ? $length : '-')
. q! "! . _string($env->{"HTTP_" . uc('Referer')})
. q!" "! . _string($env->{"HTTP_" . uc('User_agent')})
. q!"!
}