Skip to content

Commit ae973f6

Browse files
committed
fix: apply Options service fallback when event service is unset
Client::prepareEvent used `$event->getService() ?: $options->getService()`, but Event::__construct defaults `$service` to the string `'unknown'`, which is truthy — so the Elvis operator never fell through to the option. Calls like `LogtideSdk::init(['service' => 'my-php-app'])` followed by `captureLog(...)` therefore always shipped `service: "unknown"`. The fallback now triggers when the event service is `''` or `'unknown'`. Updated `testCaptureEventFallsToOptionsServiceWhenUnknown`, which had been written to assert the broken behavior. Closes logtide-dev/logtide-php#7.
1 parent 0bfa7c6 commit ae973f6

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.7.4] - 2026-04-14
9+
10+
### Fixed
11+
12+
- Fixed `Options::$service` being silently ignored for any log captured without an explicit per-call service. `Client::prepareEvent` used `$event->getService() ?: $this->options->getService()`, but `Event::__construct` defaults `$service` to the string `'unknown'`, which is truthy — so the Elvis operator never fell through to the option. As a result, calls like `LogtideSdk::init(['service' => 'my-php-app'])` followed by `captureLog(...)` always shipped `service: "unknown"`. The fallback now triggers when the event service is `''` or `'unknown'`. Closes [#7](https://github.com/logtide-dev/logtide-php/issues/7).
13+
814
## [0.7.3] - 2026-04-07
915

1016
### Fixed

packages/logtide/src/Client.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ public function getOptions(): Options
124124

125125
private function prepareEvent(Event $event, ?Scope $scope, ?EventHint $hint): ?Event
126126
{
127-
$event->setService($event->getService() ?: $this->options->getService());
127+
$service = $event->getService();
128+
if ($service === '' || $service === 'unknown') {
129+
$event->setService($this->options->getService());
130+
}
128131

129132
if ($this->options->getEnvironment() !== null) {
130133
$event->setEnvironment($this->options->getEnvironment());

packages/logtide/tests/Unit/ClientTest.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ public function testCaptureEventSetsServiceFromOptions(): void
7474
$transport = $this->createSpyTransport();
7575
$client = $this->createClient(['service' => 'my-api'], $transport);
7676

77-
// Event created without explicit service defaults to 'unknown',
78-
// which is falsy in ?: so Options service is applied
7977
$event = new Event(LogLevel::INFO, 'test');
8078
$event->setService('');
8179
$client->captureEvent($event);
@@ -99,14 +97,9 @@ public function testCaptureEventFallsToOptionsServiceWhenUnknown(): void
9997
$transport = $this->createSpyTransport();
10098
$client = $this->createClient(['service' => 'my-api'], $transport);
10199

102-
// captureLog without explicit service creates event with 'unknown' service,
103-
// but 'unknown' is truthy so ?: won't fall through. The real service override
104-
// happens when passing null to captureLog
105100
$client->captureLog(LogLevel::INFO, 'test', [], null, null);
106101

107-
// With no service override in captureLog, the event gets 'unknown' from Event constructor
108-
// which is truthy, so it stays as 'unknown'
109-
$this->assertSame('unknown', $transport->sentLogs[0]->getService());
102+
$this->assertSame('my-api', $transport->sentLogs[0]->getService());
110103
}
111104

112105
public function testCaptureEventSetsEnvironmentFromOptions(): void

0 commit comments

Comments
 (0)