Skip to content

Commit

Permalink
Change logic for disabling logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Hectorhammett committed Sep 20, 2024
1 parent 975c564 commit 7e76353
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
7 changes: 3 additions & 4 deletions src/HttpHandler/HttpHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ class HttpHandlerFactory
* Builds out a default http handler for the installed version of guzzle.
*
* @param ClientInterface $client
* @param LoggerInterface $logger
* @param null|false|LoggerInterface $logger
* @param bool $loggerDisabled
* @return Guzzle6HttpHandler|Guzzle7HttpHandler
* @throws \Exception
*/
public static function build(

Check failure on line 38 in src/HttpHandler/HttpHandlerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis / PHPStan Static Analysis

PHPDoc tag @param references unknown parameter: $loggerDisabled
ClientInterface $client = null,
LoggerInterface $logger = null,
bool $loggerDisabled = false
null|false|LoggerInterface $logger = null,
)
{
if (is_null($client)) {
Expand All @@ -53,7 +52,7 @@ public static function build(
$client = new Client(['handler' => $stack]);
}

if (!$loggerDisabled) {
if ($logger !== false) {
$logger = $logger ?? ApplicationDefaultCredentials::getDefaultLogger();
}

Expand Down
18 changes: 11 additions & 7 deletions src/Logging/LoggingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,19 @@ private function logRequest(LogEvent $event): void
'timestamp' => $event->timestamp,
'severity' => strtoupper(LogLevel::DEBUG),
'clientId' => $event->clientId,
'requestId' => $event->requestId,
'requestId' => $event->requestId ?? null,
];

$jsonPayload = [
'request.method' => $event->method,
'request.url' => $event->url,
'request.headers' => $event->headers,
'request.payload' => $event->payload,
'request.jwt' => $this->getJwtToken($event->headers),
'request.jwt' => $this->getJwtToken($event->headers ?? []),
'retryAttempt' => $event->retryAttempt
];

// Filter out the falsey values
$jsonPayload = array_filter($jsonPayload);
$debugEvent['jsonPayload'] = $jsonPayload;
$debugEvent['jsonPayload'] = array_filter($jsonPayload);

$this->logger->debug((string) json_encode($debugEvent));
}
Expand All @@ -52,26 +50,28 @@ private function logResponse(LogEvent $event): void
'timestamp' => $event->timestamp,
'severity' => strtoupper(LogLevel::DEBUG),
'clientId' => $event->clientId,
'requestId' => $event->requestId,
'requestId' => $event->requestId ?? null,
'jsonPayload' => [
'response.headers' => $event->headers,
'response.payload' => $event->payload,
'latency' => $event->latency,
]
];

$debugEvent['jsonPayload'] = array_filter($debugEvent['jsonPayload']);
$this->logger->debug((string) json_encode($debugEvent));

$infoEvent = [
'timestamp' => $event->timestamp,
'severity' => LogLevel::INFO,
'clientId' => $event->clientId,
'requestId' => $event->requestId,
'requestId' => $event->requestId ?? null,
'jsonPayload' => [
'response.status' => $event->status
]
];

$infoEvent['jsonPayload'] = array_filter($infoEvent['jsonPayload']);
$this->logger->info((string) json_encode($infoEvent));
}

Expand All @@ -80,6 +80,10 @@ private function logResponse(LogEvent $event): void
*/
private function getJwtToken(array $headers): null|array
{
if (empty($headers)) {
return null;
}

$tokenHeader = $headers['Authorization'] ?? '';
$token = str_replace('Bearer ', '', $tokenHeader);

Expand Down

0 comments on commit 7e76353

Please sign in to comment.