Configuration
1 year ago
ApiLogger.php
1 year ago
ConsoleLogger.php
1 year ago
LoggerConstants.php
1 year ago
NullApiLogger.php
1 year ago
ApiLogger.php
116 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Core\Logger; |
| 4 | |
| 5 | use Core\Logger\Configuration\LoggingConfiguration; |
| 6 | use CoreInterfaces\Core\Logger\ApiLoggerInterface; |
| 7 | use CoreInterfaces\Core\Request\RequestInterface; |
| 8 | use CoreInterfaces\Core\Response\ResponseInterface; |
| 9 | |
| 10 | class ApiLogger implements ApiLoggerInterface |
| 11 | { |
| 12 | private $config; |
| 13 | |
| 14 | public function __construct(LoggingConfiguration $config) |
| 15 | { |
| 16 | $this->config = $config; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * @inheritDoc |
| 21 | */ |
| 22 | public function logRequest(RequestInterface $request): void |
| 23 | { |
| 24 | $contentType = $this->getHeaderValue(LoggerConstants::CONTENT_TYPE_HEADER, $request->getHeaders()); |
| 25 | |
| 26 | $this->config->logMessage( |
| 27 | 'Request {' . LoggerConstants::METHOD . '} {' . LoggerConstants::URL . |
| 28 | '} {' . LoggerConstants::CONTENT_TYPE . '}', |
| 29 | [ |
| 30 | LoggerConstants::METHOD => $request->getHttpMethod(), |
| 31 | LoggerConstants::URL => $this->getRequestUrl($request), |
| 32 | LoggerConstants::CONTENT_TYPE => $contentType |
| 33 | ] |
| 34 | ); |
| 35 | |
| 36 | if ($this->config->getRequestConfig()->shouldLogHeaders()) { |
| 37 | $headers = $this->config->getRequestConfig()->getLoggableHeaders( |
| 38 | $request->getHeaders(), |
| 39 | $this->config->shouldMaskSensitiveHeaders() |
| 40 | ); |
| 41 | $this->config->logMessage( |
| 42 | 'Request Headers {' . LoggerConstants::HEADERS . '}', |
| 43 | [LoggerConstants::HEADERS => $headers] |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | if ($this->config->getRequestConfig()->shouldLogBody()) { |
| 48 | $body = $request->getParameters(); |
| 49 | if (empty($body)) { |
| 50 | $body = $request->getBody(); |
| 51 | } |
| 52 | $this->config->logMessage( |
| 53 | 'Request Body {' . LoggerConstants::BODY . '}', |
| 54 | [LoggerConstants::BODY => $body] |
| 55 | ); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @inheritDoc |
| 61 | */ |
| 62 | public function logResponse(ResponseInterface $response): void |
| 63 | { |
| 64 | $contentLength = $this->getHeaderValue(LoggerConstants::CONTENT_LENGTH_HEADER, $response->getHeaders()); |
| 65 | $contentType = $this->getHeaderValue(LoggerConstants::CONTENT_TYPE_HEADER, $response->getHeaders()); |
| 66 | |
| 67 | $this->config->logMessage( |
| 68 | 'Response {' . LoggerConstants::STATUS_CODE . '} {' . LoggerConstants::CONTENT_LENGTH . |
| 69 | '} {' . LoggerConstants::CONTENT_TYPE . '}', |
| 70 | [ |
| 71 | LoggerConstants::STATUS_CODE => $response->getStatusCode(), |
| 72 | LoggerConstants::CONTENT_LENGTH => $contentLength, |
| 73 | LoggerConstants::CONTENT_TYPE => $contentType |
| 74 | ] |
| 75 | ); |
| 76 | |
| 77 | if ($this->config->getResponseConfig()->shouldLogHeaders()) { |
| 78 | $headers = $this->config->getResponseConfig()->getLoggableHeaders( |
| 79 | $response->getHeaders(), |
| 80 | $this->config->shouldMaskSensitiveHeaders() |
| 81 | ); |
| 82 | $this->config->logMessage( |
| 83 | 'Response Headers {' . LoggerConstants::HEADERS . '}', |
| 84 | [LoggerConstants::HEADERS => $headers] |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | if ($this->config->getResponseConfig()->shouldLogBody()) { |
| 89 | $this->config->logMessage( |
| 90 | 'Response Body {' . LoggerConstants::BODY . '}', |
| 91 | [LoggerConstants::BODY => $response->getRawBody()] |
| 92 | ); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | private function getHeaderValue(string $key, array $headers): ?string |
| 97 | { |
| 98 | $key = strtolower($key); |
| 99 | foreach ($headers as $k => $value) { |
| 100 | if (strtolower($k) === $key) { |
| 101 | return $value; |
| 102 | } |
| 103 | } |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | private function getRequestUrl(RequestInterface $request): string |
| 108 | { |
| 109 | $queryUrl = $request->getQueryUrl(); |
| 110 | if ($this->config->getRequestConfig()->shouldIncludeQueryInPath()) { |
| 111 | return $queryUrl; |
| 112 | } |
| 113 | return explode("?", $queryUrl)[0]; |
| 114 | } |
| 115 | } |
| 116 |