diff --git a/src/Logging/LoggingTrait.php b/src/Logging/LoggingTrait.php index 41c6304ae..3575e489b 100644 --- a/src/Logging/LoggingTrait.php +++ b/src/Logging/LoggingTrait.php @@ -22,7 +22,7 @@ trait LoggingTrait { /** - * @param LogEvent $event + * @param RpcLogEvent $event */ private function logRequest(RpcLogEvent $event): void { @@ -39,7 +39,7 @@ private function logRequest(RpcLogEvent $event): void 'request.method' => $event->method, 'request.url' => $event->url, 'request.headers' => $event->headers, - 'request.payload' => $event->payload, + 'request.payload' => $this->truncatePayload($event->payload), 'request.jwt' => $this->getJwtToken($event->headers ?? []), 'retryAttempt' => $event->retryAttempt ]; @@ -58,7 +58,7 @@ private function logRequest(RpcLogEvent $event): void } /** - * @param LogEvent $event + * @param RpcLogEvent $event */ private function logResponse(RpcLogEvent $event): void { @@ -69,7 +69,7 @@ private function logResponse(RpcLogEvent $event): void 'requestId' => $event->requestId ?? null, 'jsonPayload' => [ 'response.headers' => $event->headers, - 'response.payload' => $event->payload, + 'response.payload' => $this->truncatePayload($event->payload), 'latency' => $event->latency, ] ]; @@ -89,32 +89,12 @@ private function logResponse(RpcLogEvent $event): void } if (!is_null($event->status)) { - $infoEvent = [ - 'timestamp' => $event->timestamp, - 'severity' => LogLevel::INFO, - 'clientId' => $event->clientId, - 'requestId' => $event->requestId ?? null, - 'jsonPayload' => [ - 'response.status' => $event->status - ] - ]; - - // Remove null values - $infoEvent = array_filter($infoEvent, fn ($value) => !is_null($value)); - - $stringifiedEvent = json_encode($infoEvent); - - // There was an error stringifying the event, return to not break execution - if ($stringifiedEvent === false) { - return; - } - - $this->logger->info($stringifiedEvent); + $this->logStatus($event); } } /** - * @param LogEvent $event + * @param RpcLogEvent $event */ private function logStatus(RpcLogEvent $event): void { @@ -168,4 +148,17 @@ private function getJwtToken(array $headers): null|array 'token' => base64_decode($token) ]; } + + /** + * @param string $payload + * @return string + */ + private function truncatePayload(string $payload): string + { + if (strlen($payload) <= 500) { + return $payload; + } + + return substr($payload, 0, 500) . '...'; + } } diff --git a/src/Logging/RpcLogEvent.php b/src/Logging/RpcLogEvent.php index 684fca35c..7080f5b78 100644 --- a/src/Logging/RpcLogEvent.php +++ b/src/Logging/RpcLogEvent.php @@ -57,9 +57,9 @@ class RpcLogEvent /** * An array representation of JSON for the response or request * - * @var null|string|array + * @var null|string */ - public null|string|array $payload = null; + public null|string $payload = null; /** * Status code for REST or gRPC methods diff --git a/tests/Logging/LoggingTraitTest.php b/tests/Logging/LoggingTraitTest.php index 41dd56af0..3fd215b0e 100644 --- a/tests/Logging/LoggingTraitTest.php +++ b/tests/Logging/LoggingTraitTest.php @@ -102,7 +102,7 @@ private function getNewLogEvent(): RpcLogEvent 'header1' => 'test', 'Authorization' => 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.cThIIoDvwdueQB468K5xDc5633seEFoqwxjF_xSJyQQ' ]; - $event->payload = ['param' => 'test']; + $event->payload = json_encode(['param' => 'test']); $event->status = 200; $event->retryAttempt = 0; $event->rpcName = 'Rpc NameTest';