Skip to content

Commit 115d1fc

Browse files
authored
ref(logs): add sentry.origin attribute to monolog handler (#1917)
1 parent ac91687 commit 115d1fc

2 files changed

Lines changed: 30 additions & 18 deletions

File tree

src/Monolog/LogsHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function handle($record): bool
6666
self::getSentryLogLevelFromMonologLevel($record['level']),
6767
$record['message'],
6868
[],
69-
array_merge($record['context'], $record['extra'])
69+
array_merge($record['context'], $record['extra'], ['sentry.origin' => 'auto.logger.monolog'])
7070
);
7171

7272
return $this->bubble === false;

tests/Monolog/LogsHandlerTest.php

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ final class LogsHandlerTest extends TestCase
2323
protected function setUp(): void
2424
{
2525
Logs::getInstance()->flush();
26-
}
27-
28-
/**
29-
* @dataProvider handleDataProvider
30-
*/
31-
public function testHandle($record, Log $expectedLog): void
32-
{
3326
$client = ClientBuilder::create([
3427
'enable_logs' => true,
3528
'before_send' => static function () {
@@ -39,7 +32,13 @@ public function testHandle($record, Log $expectedLog): void
3932

4033
$hub = new Hub($client);
4134
SentrySdk::setCurrentHub($hub);
35+
}
4236

37+
/**
38+
* @dataProvider handleDataProvider
39+
*/
40+
public function testHandle($record, Log $expectedLog): void
41+
{
4342
$handler = new LogsHandler();
4443
$handler->handle($record);
4544

@@ -69,16 +68,6 @@ static function (string $key) {
6968
*/
7069
public function testLogLevels($record, int $countLogs): void
7170
{
72-
$client = ClientBuilder::create([
73-
'enable_logs' => true,
74-
'before_send' => static function () {
75-
return null;
76-
},
77-
])->getClient();
78-
79-
$hub = new Hub($client);
80-
SentrySdk::setCurrentHub($hub);
81-
8271
$handler = new LogsHandler(LogLevel::warn());
8372
$handler->handle($record);
8473

@@ -131,6 +120,29 @@ private function handleLogAndDrop(): void
131120
$handler->handle(RecordFactory::create('I was dropped :(', Logger::INFO, 'chanel.foo', [], []));
132121
}
133122

123+
public function testOriginTagAppliedWithHandler(): void
124+
{
125+
$handler = new LogsHandler(LogLevel::warn());
126+
$handler->handle(RecordFactory::create('with origin', Logger::WARNING, 'channel.foo', [], []));
127+
128+
$logs = Logs::getInstance()->aggregator()->all();
129+
$this->assertCount(1, $logs);
130+
$log = $logs[0];
131+
$this->assertArrayHasKey('sentry.origin', $log->attributes()->toSimpleArray());
132+
$this->assertSame('auto.logger.monolog', $log->attributes()->toSimpleArray()['sentry.origin']);
133+
}
134+
135+
public function testOriginTagNotAppliedWhenUsingDirectly()
136+
{
137+
\Sentry\logger()->info('No origin attribute');
138+
139+
$logs = Logs::getInstance()->aggregator()->all();
140+
$this->assertCount(1, $logs);
141+
$log = $logs[0];
142+
$this->assertSame('No origin attribute', $log->getBody());
143+
$this->assertArrayNotHasKey('sentry.origin', $log->attributes()->toSimpleArray());
144+
}
145+
134146
public static function handleDataProvider(): iterable
135147
{
136148
yield [

0 commit comments

Comments
 (0)