1Apache::LogFormat::CompUisleerr(C3opnmt)ributed Perl DocAupmaecnhtea:t:iLoongFormat::Compiler(3pm)
2
3
4

NAME

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

SYNOPSIS

9         use Apache::LogFormat::Compiler;
10
11         my $log_handler = Apache::LogFormat::Compiler->new("combined");
12         my $log = $log_handler->log_line(
13             $env,
14             $res,
15             $length,
16             $reqtime,
17             $time
18         );
19

DESCRIPTION

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

METHOD

25       new($fmt:String)
26           Takes a format string (or a preset template "combined" or "custom")
27           to specify the log format. This module implements a subset of
28           Apache's LogFormat templates
29           <http://httpd.apache.org/docs/2.0/mod/mod_log_config.html>:
30
31              %%    a percent sign
32              %h    REMOTE_ADDR from the PSGI environment, or -
33              %l    remote logname not implemented (currently always -)
34              %u    REMOTE_USER from the PSGI environment, or -
35              %t    [local timestamp, in default format]
36              %r    REQUEST_METHOD, REQUEST_URI and SERVER_PROTOCOL from the PSGI environment
37              %s    the HTTP status code of the response
38              %b    content length of the response
39              %T    custom field for handling times in subclasses
40              %D    custom field for handling sub-second times in subclasses
41              %v    SERVER_NAME from the PSGI environment, or -
42              %V    HTTP_HOST or SERVER_NAME from the PSGI environment, or -
43              %p    SERVER_PORT from the PSGI environment
44              %P    the worker's process id
45              %m    REQUEST_METHOD from the PSGI environment
46              %U    PATH_INFO from the PSGI environment
47              %q    QUERY_STRING from the PSGI environment
48              %H    SERVER_PROTOCOL from the PSGI environment
49
50           In addition, custom values can be referenced, using "%{name}", with
51           one of the mandatory modifier flags "i", "o" or "t":
52
53              %{variable-name}i    HTTP_VARIABLE_NAME value from the PSGI environment
54              %{header-name}o      header-name header in the response
55              %{time-format]t      localtime in the specified strftime format
56
57       log_line($env:HashRef, $res:ArrayRef, $length:Integer,
58       $reqtime:Integer, $time:Integer): $log:String
59           Generates log line.
60
61             $env      PSGI env request HashRef
62             $res      PSGI response ArrayRef
63             $length   Content-Length
64             $reqtime  The time taken to serve request in microseconds. optional
65             $time     Time the request was received. optional. If $time is undefined. current timestamp is used.
66
67           Sample psgi
68
69             use Plack::Builder;
70             use Time::HiRes;
71             use Apache::LogFormat::Compiler;
72
73             my $log_handler = Apache::LogFormat::Compiler->new(
74                 '%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-agent}i" %D'
75             );
76             my $compile_log_app = builder {
77                 enable sub {
78                     my $app = shift;
79                     sub {
80                         my $env = shift;
81                         my $t0 = [gettimeofday];
82                         my $res = $app->();
83                         my $reqtime = int(Time::HiRes::tv_interval($t0) * 1_000_000);
84                         $env->{psgi.error}->print($log_handler->log_line(
85                             $env,$res,6,$reqtime, $t0->[0]));
86                     }
87                 };
88                 $app
89             };
90

ABOUT POSIX::strftime::Compiler

92       This module uses POSIX::strftime::Compiler for generate datetime
93       string. POSIX::strftime::Compiler provides GNU C library compatible
94       strftime(3). But this module will not affected by the system locale.
95       This feature is useful when you want to write loggers, servers and
96       portable applications.
97

ADD CUSTOM FORMAT STRING

99       Apache::LogFormat::Compiler allows one to add a custom format string
100
101         my $log_handler = Apache::LogFormat::Compiler->new(
102             '%z %{HTTP_X_FORWARDED_FOR|REMOTE_ADDR}Z',
103             char_handlers => +{
104                 'z' => sub {
105                     my ($env,$req) = @_;
106                     return $env->{HTTP_X_FORWARDED_FOR};
107                 }
108             },
109             block_handlers => +{
110                 'Z' => sub {
111                     my ($block,$env,$req) = @_;
112                     # block eq 'HTTP_X_FORWARDED_FOR|REMOTE_ADDR'
113                     my ($main, $alt) = split('\|', $args);
114                     return exists $env->{$main} ? $env->{$main} : $env->{$alt};
115                 }
116             },
117         );
118
119       Any single letter can be used, other than those already defined by
120       Apache::LogFormat::Compiler.  Your sub is called with two or three
121       arguments: the content inside the "{}" from the format (block_handlers
122       only), the PSGI environment ($env), and the ArrayRef of the response.
123       It should return the string to be logged.
124

AUTHOR

126       Masahiro Nagano <kazeburo@gmail.com>
127

SEE ALSO

129       Plack::Middleware::AccessLog,
130       <http://httpd.apache.org/docs/2.2/mod/mod_log_config.html>
131

LICENSE

133       Copyright (C) Masahiro Nagano
134
135       This library is free software; you can redistribute it and/or modify it
136       under the same terms as Perl itself.
137
138
139
140perl v5.34.0                      2022-01-20  Apache::LogFormat::Compiler(3pm)
Impressum