1Plack::Middleware::AcceUssseLrogC(o3n)tributed Perl DocuPmleanctka:t:iMoinddleware::AccessLog(3)
2
3
4
6 Plack::Middleware::AccessLog - Logs requests like Apache's log format
7
9 # in app.psgi
10 use Plack::Builder;
11
12 builder {
13 enable "Plack::Middleware::AccessLog", format => "combined";
14 $app;
15 };
16
18 Plack::Middleware::AccessLog forwards the request to the given app and
19 logs request and response details to the logger callback. The format
20 can be specified using Apache-like format strings (or "combined" or
21 "common" for the default formats). If none is specified "combined" is
22 used.
23
24 This middleware uses calculable Content-Length by checking body type,
25 and cannot log the time taken to serve requests. It also logs the
26 request before the response is actually sent to the client. Use
27 Plack::Middleware::AccessLog::Timed if you want to log details after
28 the response is transmitted (more like a real web server) to the
29 client.
30
31 This middleware is enabled by default when you run plackup as a default
32 "development" environment.
33
35 format
36 enable "Plack::Middleware::AccessLog",
37 format => '%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-agent}i"';
38
39 Takes a format string (or a preset template "combined" or "custom")
40 to specify the log format. This middleware uses
41 Apache::LogFormat::Compiler to generate access_log lines. See more
42 details on perldoc Apache::LogFormat::Compiler
43
44 %% a percent sign
45 %h REMOTE_ADDR from the PSGI environment, or -
46 %l remote logname not implemented (currently always -)
47 %u REMOTE_USER from the PSGI environment, or -
48 %t [local timestamp, in default format]
49 %r REQUEST_METHOD, REQUEST_URI and SERVER_PROTOCOL from the PSGI environment
50 %s the HTTP status code of the response
51 %b content length of the response
52 %T custom field for handling times in subclasses
53 %D custom field for handling sub-second times in subclasses
54 %v SERVER_NAME from the PSGI environment, or -
55 %V HTTP_HOST or SERVER_NAME from the PSGI environment, or -
56 %p SERVER_PORT from the PSGI environment
57 %P the worker's process id
58 %m REQUEST_METHOD from the PSGI environment
59 %U PATH_INFO from the PSGI environment
60 %q QUERY_STRING from the PSGI environment
61 %H SERVER_PROTOCOL from the PSGI environment
62
63 Some of these format fields are only supported by middleware that
64 subclasses "AccessLog".
65
66 In addition, custom values can be referenced, using "%{name}", with
67 one of the mandatory modifier flags "i", "o" or "t":
68
69 %{variable-name}i HTTP_VARIABLE_NAME value from the PSGI environment
70 %{header-name}o header-name header in the response
71 %{time-format]t localtime in the specified strftime format
72
73 logger
74 my $logger = Log::Dispatch->new(...);
75 enable "Plack::Middleware::AccessLog",
76 logger => sub { $logger->log(level => 'debug', message => @_) };
77
78 Sets a callback to print log message to. It prints to the
79 "psgi.errors" output stream by default.
80
81 char_handlers
82 my $handlers = {
83 'z' => sub {
84 my ($env,$req) = @_;
85 return $env->{HTTP_X_FORWARDED_FOR};
86 }
87 };
88
89 enable "Plack::Middleware::AccessLog",
90 format => '%z %{HTTP_X_FORWARDED_FOR|REMOTE_ADDR}Z',
91 char_handlers => $handlers;
92
93 Takes a hash reference and passes it to the underlying
94 Apache::LogFormat::Compiler's "char_handlers". For more details
95 see "ADD CUSTOM FORMAT STRING" in Apache::LogFormat::Compiler.
96
97 block_handlers
98 my $handlers = {
99 'Z' => sub {
100 my ($block,$env,$req) = @_;
101 # block eq 'HTTP_X_FORWARDED_FOR|REMOTE_ADDR'
102 my ($main, $alt) = split('\|', $args);
103 return exists $env->{$main} ? $env->{$main} : $env->{$alt};
104 }
105 };
106
107 enable "Plack::Middleware::AccessLog",
108 format => '%z %{HTTP_X_FORWARDED_FOR|REMOTE_ADDR}Z',
109 block_handlers => $handlers;
110
111 Takes a hash reference and passes it to the underlying
112 Apache::LogFormat::Compiler's "block_handlers". For more details
113 see "ADD CUSTOM FORMAT STRING" in Apache::LogFormat::Compiler.
114
116 Tatsuhiko Miyagawa
117
118 Masahiro Nagano
119
121 Apache::LogFormat::Compiler,
122 <http://httpd.apache.org/docs/2.2/mod/mod_log_config.html>
123 Rack::CustomLogger
124
125
126
127perl v5.36.0 2023-01-20 Plack::Middleware::AccessLog(3)