-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathLoggingMiddleware.php
60 lines (48 loc) · 1.58 KB
/
LoggingMiddleware.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
<?php
/**
* This file is part of the tarantool/client package.
*
* (c) Eugene Leonovich <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Tarantool\Client\Middleware;
use Psr\Log\LoggerInterface;
use Tarantool\Client\Handler\Handler;
use Tarantool\Client\Request\Request;
use Tarantool\Client\RequestTypes;
use Tarantool\Client\Response;
final class LoggingMiddleware implements Middleware
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function process(Request $request, Handler $handler) : Response
{
$requestName = RequestTypes::getName($request->getType());
$this->logger->debug("Starting handling request \"$requestName\"", [
'request' => $request,
]);
$start = \microtime(true);
try {
$response = $handler->handle($request);
} catch (\Throwable $e) {
$this->logger->error("Request \"$requestName\" failed", [
'request' => $request,
'exception' => $e,
'duration_ms' => \round((\microtime(true) - $start) * 1000),
]);
throw $e;
}
$this->logger->debug("Finished handling request \"$requestName\"", [
'request' => $request,
'response' => $response,
'duration_ms' => \round((\microtime(true) - $start) * 1000),
]);
return $response;
}
}