From fb0ac539a232a1afe4fde2ecaf544723b427fc5d Mon Sep 17 00:00:00 2001 From: Hector Mendoza Jacobo Date: Tue, 8 Oct 2024 19:21:55 +0000 Subject: [PATCH] Handle false for the case when json_encode returns false --- src/Logging/LoggingTrait.php | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Logging/LoggingTrait.php b/src/Logging/LoggingTrait.php index 0cc258c88..db416e867 100644 --- a/src/Logging/LoggingTrait.php +++ b/src/Logging/LoggingTrait.php @@ -44,9 +44,17 @@ private function logRequest(LogEvent $event): void 'retryAttempt' => $event->retryAttempt ]; + // Remove null values $debugEvent['jsonPayload'] = array_filter($jsonPayload, fn ($value) => !is_null($value)); - $this->logger->debug((string) json_encode($debugEvent)); + $stringifiedEvent = json_encode($debugEvent); + + // There was an error stringifying the event, return to not break execution + if ($stringifiedEvent === false) { + return; + } + + $this->logger->debug($stringifiedEvent); } /** @@ -66,12 +74,21 @@ private function logResponse(LogEvent $event): void ] ]; + // Remove null values $debugEvent = array_filter($debugEvent, fn ($value) => !is_null($value)); $debugEvent['jsonPayload'] = array_filter( $debugEvent['jsonPayload'], fn ($value) => !is_null($value) ); - $this->logger->debug((string) json_encode($debugEvent)); + + $stringifiedEvent = json_encode($debugEvent); + + // There was an error stringifying the event, return to not break execution + if ($stringifiedEvent === false) { + return; + } + + $this->logger->debug($stringifiedEvent); if ($event->status) { $infoEvent = [ @@ -84,9 +101,17 @@ private function logResponse(LogEvent $event): void ] ]; + // Remove null values $infoEvent = array_filter($infoEvent, fn ($value) => !is_null($value)); - $this->logger->info((string) json_encode($infoEvent)); + $stringifiedEvent = json_encode($debugEvent); + + // There was an error stringifying the event, return to not break execution + if ($stringifiedEvent === false) { + return; + } + + $this->logger->info($stringifiedEvent); } }