-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAccessLog.php
254 lines (226 loc) · 8.26 KB
/
AccessLog.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
declare(strict_types = 1);
namespace Middlewares;
use Middlewares\AccessLogFormats as Format;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
class AccessLog implements MiddlewareInterface
{
/**
* @link http://httpd.apache.org/docs/2.4/mod/mod_log_config.html#examples
*/
const FORMAT_COMMON = '%h %l %u %t "%r" %>s %b';
const FORMAT_COMMON_VHOST = '%v %h %l %u %t "%r" %>s %b';
const FORMAT_COMBINED = '%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"';
const FORMAT_REFERER = '%{Referer}i -> %U';
const FORMAT_AGENT = '%{User-Agent}i';
/**
* @link https://httpd.apache.org/docs/2.4/logs.html#virtualhost
*/
const FORMAT_VHOST = '%v %l %u %t "%r" %>s %b';
/**
* @link https://anonscm.debian.org/cgit/pkg-apache/apache2.git/tree/debian/config-dir/apache2.conf.in#n212
*/
const FORMAT_COMMON_DEBIAN = '%h %l %u %t “%r” %>s %O';
const FORMAT_COMBINED_DEBIAN = '%h %l %u %t “%r” %>s %O “%{Referer}i” “%{User-Agent}i”';
const FORMAT_VHOST_COMBINED_DEBIAN = '%v:%p %h %l %u %t “%r” %>s %O “%{Referer}i” “%{User-Agent}i"';
/**
* @var LoggerInterface The router container
*/
private $logger;
/**
* @var callable
*/
private $context;
/**
* @var string
*/
private $format = self::FORMAT_COMMON;
/**
* @var string|null
*/
private $ipAttribute;
/**
* @var bool
*/
private $hostnameLookups = false;
/**
* Set the LoggerInterface instance.
*/
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* Set the desired format
*/
public function format(string $format): self
{
$this->format = $format;
return $this;
}
/**
* Set the attribute name to get the client ip.
*/
public function ipAttribute(string $ipAttribute): self
{
$this->ipAttribute = $ipAttribute;
return $this;
}
/**
* Set the hostname lookups flag
*/
public function hostnameLookups(bool $hostnameLookups = true): self
{
$this->hostnameLookups = $hostnameLookups;
return $this;
}
/**
* Set context callable
*/
public function context(callable $context): self
{
$this->context = $context;
return $this;
}
/**
* Process a server request and return a response.
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$begin = microtime(true);
$response = $handler->handle($request);
$end = microtime(true);
$message = $this->format;
$message = $this->replaceConstantDirectives($message, $request, $response, $begin, $end);
$message = $this->replaceVariableDirectives($message, $request, $response, $begin, $end);
$context = [];
if ($this->context !== null) {
$contextFunction = $this->context;
$context = $contextFunction($request, $response, $message);
}
$statusCode = $response->getStatusCode();
if ($statusCode >= 400 && $statusCode < 500) {
$this->logger->warning($message, $context);
} elseif ($statusCode >= 500 && $statusCode < 600) {
$this->logger->error($message, $context);
} else {
$this->logger->info($message, $context);
}
return $response;
}
private function replaceConstantDirectives(
string $format,
ServerRequestInterface $request,
ResponseInterface $response,
float $begin,
float $end
): string {
return preg_replace_callback(
'/%(?:[<>])?([%aABbDfhHklLmpPqrRstTuUvVXIOS])/',
function (array $matches) use ($request, $response, $begin, $end) {
switch ($matches[1]) {
case '%':
return '%';
case 'a':
return Format::getClientIp($request, $this->ipAttribute);
case 'A':
return Format::getLocalIp($request);
case 'B':
return Format::getBodySize($response, '0');
case 'b':
return Format::getBodySize($response, '-');
case 'D':
return Format::getRequestDuration($begin, $end, 'ms');
case 'f':
return Format::getFilename($request);
case 'h':
return Format::getRemoteHostname($request, $this->hostnameLookups);
case 'H':
return Format::getProtocol($request);
case 'm':
return Format::getMethod($request);
case 'p':
return Format::getPort($request, 'canonical');
case 'q':
return Format::getQuery($request);
case 'r':
return Format::getRequestLine($request);
case 's':
return Format::getStatus($response);
case 't':
return Format::getRequestTime($begin, $end, 'begin:d/M/Y:H:i:s O');
case 'T':
return Format::getRequestDuration($begin, $end, 's');
case 'u':
return Format::getRemoteUser($request);
case 'U':
return Format::getPath($request);
case 'v':
return Format::getHost($request);
case 'V':
return Format::getServerName($request);
case 'I':
$messageSize = Format::getMessageSize($request);
return null === $messageSize ? '-' : (string) $messageSize;
case 'O':
$messageSize = Format::getMessageSize($response);
return null === $messageSize ? '-' : (string) $messageSize;
case 'S':
return Format::getTransferredSize($request, $response);
//NOT IMPLEMENTED
case 'k':
case 'l':
case 'L':
case 'P':
case 'R':
case 'X':
default:
return '-';
}
},
$format
);
}
private function replaceVariableDirectives(
string $format,
ServerRequestInterface $request,
ResponseInterface $response,
float $begin,
float $end
): string {
return preg_replace_callback(
'/%(?:[<>])?{([^}]+)}([aCeinopPtT])/',
function (array $matches) use ($request, $response, $begin, $end) {
switch ($matches[2]) {
case 'a':
return Format::getClientIp($request, $this->ipAttribute);
case 'C':
return Format::getCookie($request, $matches[1]);
case 'e':
return Format::getEnv($matches[1]);
case 'i':
return Format::getHeader($request, $matches[1]);
case 'n':
return Format::getAttribute($request, $matches[1]);
case 'o':
return Format::getHeader($response, $matches[1]);
case 'p':
return Format::getPort($request, $matches[1]);
case 't':
return Format::getRequestTime($begin, $end, $matches[1]);
case 'T':
return Format::getRequestDuration($begin, $end, $matches[1]);
//NOT IMPLEMENTED
case 'P':
default:
return '-';
}
},
$format
);
}
}