|
2 | 2 |
|
3 | 3 | namespace Fapi\HttpClient; |
4 | 4 |
|
5 | | -use Fapi\HttpClient\Utils\Json; |
6 | | -use Fapi\HttpClient\Utils\JsonException; |
7 | | -use Psr\Http\Message\RequestInterface; |
8 | | -use Psr\Http\Message\ResponseInterface; |
9 | | -use Throwable; |
10 | | -use function base64_encode; |
11 | | -use function extension_loaded; |
12 | | -use function function_exists; |
13 | | -use function get_class; |
14 | | -use function iconv_substr; |
15 | | -use function mb_substr; |
16 | | -use function serialize; |
17 | | -use function sprintf; |
18 | | -use function strlen; |
19 | | -use function substr; |
20 | | -use const JSON_UNESCAPED_UNICODE; |
21 | | - |
22 | | -final class BaseLoggingFormatter implements ILoggingFormatter |
| 5 | +final class BaseLoggingFormatter extends \Fapi\HttpClient\LoggingFormatters\BaseLoggingFormatter implements ILoggingFormatter |
23 | 6 | { |
24 | 7 |
|
25 | | - /** @var int */ |
26 | | - private $maxBodyLength; |
27 | | - |
28 | | - public function __construct(int $maxBodyLength = 40000) |
29 | | - { |
30 | | - $this->maxBodyLength = $maxBodyLength; |
31 | | - } |
32 | | - |
33 | | - public function formatSuccessful(RequestInterface $request, ResponseInterface $response, float $elapsedTime): string |
34 | | - { |
35 | | - return 'Fapi\HttpClient: an HTTP request has been sent.' |
36 | | - . $this->dumpHttpRequest($request) |
37 | | - . $this->dumpHttpResponse($response) |
38 | | - . $this->dumpElapsedTime($elapsedTime); |
39 | | - } |
40 | | - |
41 | | - public function formatFailed(RequestInterface $request, Throwable $exception, float $elapsedTime): string |
42 | | - { |
43 | | - return 'Fapi\HttpClient: an HTTP request failed.' |
44 | | - . $this->dumpHttpRequest($request) |
45 | | - . $this->dumpException($exception) |
46 | | - . $this->dumpElapsedTime($elapsedTime); |
47 | | - } |
48 | | - |
49 | | - private function dumpHttpRequest(RequestInterface $request): string |
50 | | - { |
51 | | - $body = $this->processBody((string) $request->getBody()); |
52 | | - |
53 | | - return ' Request URL: ' . $this->dumpValue((string) $request->getUri()) |
54 | | - . ' Request method: ' . $this->dumpValue($request->getMethod()) |
55 | | - . ' Request headers: ' . $this->dumpValue($request->getHeaders()) |
56 | | - . ' Request body: ' . $this->dumpValue($body); |
57 | | - } |
58 | | - |
59 | | - private function dumpHttpResponse(ResponseInterface $response): string |
60 | | - { |
61 | | - $body = $this->processBody((string) $response->getBody()); |
62 | | - |
63 | | - return ' Response status code: ' . $this->dumpValue($response->getStatusCode()) |
64 | | - . ' Response headers: ' . $this->dumpValue($response->getHeaders()) |
65 | | - . ' Response body: ' . $this->dumpValue($body); |
66 | | - } |
67 | | - |
68 | | - private function dumpException(Throwable $exception): string |
69 | | - { |
70 | | - $dump = ' Exception type: ' . $this->dumpValue(get_class($exception)) |
71 | | - . ' Exception message: ' . $this->dumpValue($exception->getMessage()); |
72 | | - |
73 | | - if ($exception->getPrevious() !== null) { |
74 | | - $previousException = $exception->getPrevious(); |
75 | | - |
76 | | - $dump .= ' Previous exception type: ' . $this->dumpValue(get_class($previousException)) |
77 | | - . ' Previous exception message: ' . $this->dumpValue($previousException->getMessage()); |
78 | | - } |
79 | | - |
80 | | - return $dump; |
81 | | - } |
82 | | - |
83 | | - private function dumpElapsedTime(float $elapsedTime): string |
84 | | - { |
85 | | - $elapsedTime *= 1000; |
86 | | - |
87 | | - return ' Elapsed time: ' . sprintf('%0.2f', $elapsedTime) . ' ms'; |
88 | | - } |
89 | | - |
90 | | - /** |
91 | | - * @param mixed $value |
92 | | - */ |
93 | | - private function dumpValue($value): string |
94 | | - { |
95 | | - try { |
96 | | - return Json::encode($value, JSON_UNESCAPED_UNICODE); |
97 | | - } catch (JsonException $e) { |
98 | | - return '(serialized) ' . base64_encode(serialize($value)); |
99 | | - } |
100 | | - } |
101 | | - |
102 | | - private function processBody(string $body): string |
103 | | - { |
104 | | - if (strlen($body) <= $this->maxBodyLength) { |
105 | | - return $body; |
106 | | - } |
107 | | - |
108 | | - if (function_exists('mb_substr')) { |
109 | | - return mb_substr($body, 0, $this->maxBodyLength, 'UTF-8'); |
110 | | - } |
111 | | - |
112 | | - if (!extension_loaded('iconv')) { |
113 | | - return (string) iconv_substr($body, 0, $this->maxBodyLength, 'UTF-8'); |
114 | | - } |
115 | | - |
116 | | - return substr($body, 0, $this->maxBodyLength); |
117 | | - } |
118 | | - |
119 | 8 | } |
0 commit comments