Skip to content

Commit c958dbd

Browse files
committed
ref(logs): add sentry.origin attribute to monolog handler
1 parent ab01c88 commit c958dbd

2 files changed

Lines changed: 20 additions & 18 deletions

File tree

src/Monolog/LogsHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public function handle($record): bool
6666
self::getSentryLogLevelFromMonologLevel($record['level']),
6767
$record['message'],
6868
[],
69-
array_merge($record['context'], $record['extra'])
69+
// If sentry.origin exists in other arrays, it will be overwritten.
70+
array_merge(['sentry.origin' => 'auto.logger.monolog'], $record['context'], $record['extra'])
7071
);
7172

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

tests/Monolog/LogsHandlerTest.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@ final class LogsHandlerTest extends TestCase
1919
protected function setUp(): void
2020
{
2121
Logs::getInstance()->flush();
22-
}
23-
24-
/**
25-
* @dataProvider handleDataProvider
26-
*/
27-
public function testHandle($record, Log $expectedLog): void
28-
{
2922
$client = ClientBuilder::create([
3023
'enable_logs' => true,
3124
'before_send' => static function () {
@@ -35,7 +28,13 @@ public function testHandle($record, Log $expectedLog): void
3528

3629
$hub = new Hub($client);
3730
SentrySdk::setCurrentHub($hub);
31+
}
3832

33+
/**
34+
* @dataProvider handleDataProvider
35+
*/
36+
public function testHandle($record, Log $expectedLog): void
37+
{
3938
$handler = new LogsHandler();
4039
$handler->handle($record);
4140

@@ -65,23 +64,25 @@ static function (string $key) {
6564
*/
6665
public function testLogLevels($record, int $countLogs): void
6766
{
68-
$client = ClientBuilder::create([
69-
'enable_logs' => true,
70-
'before_send' => static function () {
71-
return null;
72-
},
73-
])->getClient();
74-
75-
$hub = new Hub($client);
76-
SentrySdk::setCurrentHub($hub);
77-
7867
$handler = new LogsHandler(LogLevel::warn());
7968
$handler->handle($record);
8069

8170
$logs = Logs::getInstance()->aggregator()->all();
8271
$this->assertCount($countLogs, $logs);
8372
}
8473

74+
public function testOriginTagAppliedWithHandler(): void
75+
{
76+
$handler = new LogsHandler(LogLevel::warn());
77+
$handler->handle(RecordFactory::create('with origin', Logger::WARNING, 'channel.foo', [], []));
78+
79+
$logs = Logs::getInstance()->aggregator()->all();
80+
$this->assertCount(1, $logs);
81+
$log = $logs[0];
82+
$this->assertArrayHasKey('sentry.origin', $log->attributes()->toSimpleArray());
83+
$this->assertSame('auto.logger.monolog', $log->attributes()->toSimpleArray()['sentry.origin']);
84+
}
85+
8586
public static function handleDataProvider(): iterable
8687
{
8788
yield [

0 commit comments

Comments
 (0)