-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathApiLogger.php
115 lines (101 loc) · 3.86 KB
/
ApiLogger.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
<?php
namespace Core\Logger;
use Core\Logger\Configuration\LoggingConfiguration;
use CoreInterfaces\Core\Logger\ApiLoggerInterface;
use CoreInterfaces\Core\Request\RequestInterface;
use CoreInterfaces\Core\Response\ResponseInterface;
class ApiLogger implements ApiLoggerInterface
{
private $config;
public function __construct(LoggingConfiguration $config)
{
$this->config = $config;
}
/**
* @inheritDoc
*/
public function logRequest(RequestInterface $request): void
{
$contentType = $this->getHeaderValue(LoggerConstants::CONTENT_TYPE_HEADER, $request->getHeaders());
$this->config->logMessage(
'Request {' . LoggerConstants::METHOD . '} {' . LoggerConstants::URL .
'} {' . LoggerConstants::CONTENT_TYPE . '}',
[
LoggerConstants::METHOD => $request->getHttpMethod(),
LoggerConstants::URL => $this->getRequestUrl($request),
LoggerConstants::CONTENT_TYPE => $contentType
]
);
if ($this->config->getRequestConfig()->shouldLogHeaders()) {
$headers = $this->config->getRequestConfig()->getLoggableHeaders(
$request->getHeaders(),
$this->config->shouldMaskSensitiveHeaders()
);
$this->config->logMessage(
'Request Headers {' . LoggerConstants::HEADERS . '}',
[LoggerConstants::HEADERS => $headers]
);
}
if ($this->config->getRequestConfig()->shouldLogBody()) {
$body = $request->getParameters();
if (empty($body)) {
$body = $request->getBody();
}
$this->config->logMessage(
'Request Body {' . LoggerConstants::BODY . '}',
[LoggerConstants::BODY => $body]
);
}
}
/**
* @inheritDoc
*/
public function logResponse(ResponseInterface $response): void
{
$contentLength = $this->getHeaderValue(LoggerConstants::CONTENT_LENGTH_HEADER, $response->getHeaders());
$contentType = $this->getHeaderValue(LoggerConstants::CONTENT_TYPE_HEADER, $response->getHeaders());
$this->config->logMessage(
'Response {' . LoggerConstants::STATUS_CODE . '} {' . LoggerConstants::CONTENT_LENGTH .
'} {' . LoggerConstants::CONTENT_TYPE . '}',
[
LoggerConstants::STATUS_CODE => $response->getStatusCode(),
LoggerConstants::CONTENT_LENGTH => $contentLength,
LoggerConstants::CONTENT_TYPE => $contentType
]
);
if ($this->config->getResponseConfig()->shouldLogHeaders()) {
$headers = $this->config->getResponseConfig()->getLoggableHeaders(
$response->getHeaders(),
$this->config->shouldMaskSensitiveHeaders()
);
$this->config->logMessage(
'Response Headers {' . LoggerConstants::HEADERS . '}',
[LoggerConstants::HEADERS => $headers]
);
}
if ($this->config->getResponseConfig()->shouldLogBody()) {
$this->config->logMessage(
'Response Body {' . LoggerConstants::BODY . '}',
[LoggerConstants::BODY => $response->getRawBody()]
);
}
}
private function getHeaderValue(string $key, array $headers): ?string
{
$key = strtolower($key);
foreach ($headers as $k => $value) {
if (strtolower($k) === $key) {
return $value;
}
}
return null;
}
private function getRequestUrl(RequestInterface $request): string
{
$queryUrl = $request->getQueryUrl();
if ($this->config->getRequestConfig()->shouldIncludeQueryInPath()) {
return $queryUrl;
}
return explode("?", $queryUrl)[0];
}
}