diff --git a/src/FlagUpdatesListener.php b/src/FlagUpdatesListener.php index 81c941a..9dc48f0 100644 --- a/src/FlagUpdatesListener.php +++ b/src/FlagUpdatesListener.php @@ -118,7 +118,7 @@ public function connectAndRead(Closure $stop, ?Closure $onEvent = null): void try { $response = $this->http->sendRequest(new Request('GET', $url, $headers)); } catch (Throwable $error) { - $this->logger->warning('Failed to connect to Reflag flag updates: {error}: {message}', ['error' => $error::class, 'message' => $error->getMessage()]); + $this->logger->warning('Failed to connect to Reflag flag updates: {error}: {message}', ['error' => $error::class, 'message' => $error->getMessage(), 'exception' => $error]); return; } @@ -202,7 +202,7 @@ private function read(StreamInterface $body, Closure $stop, ?Closure $onEvent): } } catch (Throwable $error) { // Proxies and load balancers routinely end long-lived streams; like the .NET SDK, this is not a warning. - $this->logger->debug('Reflag flag updates stream failed; reconnecting: {error}: {message}', ['error' => $error::class, 'message' => $error->getMessage()]); + $this->logger->debug('Reflag flag updates stream failed; reconnecting: {error}: {message}', ['error' => $error::class, 'message' => $error->getMessage(), 'exception' => $error]); } finally { $body->close(); } @@ -242,6 +242,7 @@ private function dispatch(string $event, string $payload, ?Closure $onEvent): vo if ($version !== null) { $this->client->refreshFlags($version); + $this->logger->debug('Reflag flags updated to version {version}', ['version' => $version]); $onEvent?->__invoke('updated', ['flagStateVersion' => $version]); } } diff --git a/tests/Unit/ListenerTest.php b/tests/Unit/ListenerTest.php index 83b54a6..6605093 100644 --- a/tests/Unit/ListenerTest.php +++ b/tests/Unit/ListenerTest.php @@ -51,6 +51,7 @@ static function (string $event, array $data) use (&$updates): void { $this->assertSame([2], $updates); $this->assertSame(2, $client->flagStateVersion()); $this->assertSame([], array_values(array_filter($logger->messages(), static fn (string $m): bool => str_contains($m, 'stream failed')))); + $this->assertContains(['debug', 'Reflag flags updated to version {version}', ['version' => 2]], $logger->records); } /** Log levels follow the .NET SDK: only connect failures, bad responses and bad messages warn. */ @@ -92,6 +93,9 @@ static function (string $event) use (&$disconnects): void { ['warning', 'Failed to connect to Reflag flag updates: {error}: {message}'], ], array_map(static fn (array $r): array => [$r[0], $r[1]], $listener)); $this->assertSame(['status' => 503, 'reason' => 'Service Unavailable'], $listener[5][2]); + // PSR-3: exceptions go in the 'exception' context key so Monolog, Sentry and Flare keep the trace. + $this->assertInstanceOf(\RuntimeException::class, $listener[4][2]['exception']); + $this->assertInstanceOf(\Throwable::class, $listener[6][2]['exception']); $this->assertStringNotContainsString('not json', json_encode($logger->records), 'payloads are never logged'); // Like the .NET SDK's HandleReconnect: every successful (re)connection refetches flags, so