From 8a3b2e5a2248c5024b51629b2c4f3b9f995667bb Mon Sep 17 00:00:00 2001 From: Ron Cohen Date: Thu, 3 Dec 2026 10:46:43 +0000 Subject: [PATCH] feat: log flag updates from the listener; pass exceptions in PSR-3 context The listener now logs 'Reflag flags updated to version {version}' at debug when a flags-updated event triggers a refresh (the .NET SDK logs every refresh at info; PHP refreshes on request paths too, so only the listener logs). Connect and stream failures also pass the Throwable as 'exception' so Monolog, Sentry and Flare keep the stack trace. Other log sites still log only the exception class: store, fallback and cloud errors can carry hosts or SAS URLs. --- src/FlagUpdatesListener.php | 5 +++-- tests/Unit/ListenerTest.php | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) 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