Skip to content

Commit

Permalink
Handle false for the case when json_encode returns false
Browse files Browse the repository at this point in the history
  • Loading branch information
Hectorhammett committed Oct 8, 2024
1 parent 8699369 commit fb0ac53
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/Logging/LoggingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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 = [
Expand All @@ -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);
}
}

Expand Down

0 comments on commit fb0ac53

Please sign in to comment.