Skip to content

Commit 73c2f42

Browse files
committed
Add option to add Monolog channel to compiled log attributes
The channel is a relevant piece of metadata of a log entry. This change adds an option to include the channel in the recorded attributes, but remains backwards compatible and will not overwrite existing extra or context fields named 'channel'.
1 parent 9dc8fb0 commit 73c2f42

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/Monolog/LogsHandler.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,25 @@ class LogsHandler implements HandlerInterface
3131
*/
3232
private $bubble;
3333

34+
/**
35+
* Whether to include the channel name as an attribute in the Sentry logs.
36+
*/
37+
private bool $includeChannel;
38+
3439
/**
3540
* Creates a new Monolog handler that converts Monolog logs to Sentry logs.
3641
*
3742
* @psalm-suppress UndefinedDocblockClass
3843
*
39-
* @param LogLevel|\Monolog\Level|int|null $logLevel the minimum logging level at which this handler will be triggered and collects the logs
40-
* @param bool $bubble whether the messages that are handled can bubble up the stack or not
44+
* @param LogLevel|\Monolog\Level|int|null $logLevel the minimum logging level at which this handler will be triggered and collects the logs
45+
* @param bool $bubble whether the messages that are handled can bubble up the stack or not
46+
* @param bool $includeChannel whether to include the channel name as an attribute in the Sentry logs
4147
*/
42-
public function __construct($logLevel = null, bool $bubble = true)
48+
public function __construct($logLevel = null, bool $bubble = true, bool $includeChannel = false)
4349
{
4450
$this->logLevel = $logLevel ?? LogLevel::debug();
4551
$this->bubble = $bubble;
52+
$this->includeChannel = $includeChannel;
4653
}
4754

4855
/**
@@ -144,6 +151,11 @@ public function __destruct()
144151
*/
145152
protected function compileAttributes($record): array
146153
{
147-
return array_merge($record['context'], $record['extra'], ['sentry.origin' => 'auto.log.monolog']);
154+
return array_merge(
155+
$this->includeChannel ? ['channel' => $record['channel']] : [],
156+
$record['context'],
157+
$record['extra'],
158+
['sentry.origin' => 'auto.log.monolog']
159+
);
148160
}
149161
}

tests/Monolog/LogsHandlerTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,29 @@ public function testOriginTagAppliedWithHandler(): void
135135
$this->assertSame('auto.log.monolog', $log->attributes()->toSimpleArray()['sentry.origin']);
136136
}
137137

138+
public function testChannelNotIncludedByDefault(): void
139+
{
140+
$handler = new LogsHandler();
141+
$handler->handle(RecordFactory::create('without channel', Logger::WARNING, 'channel.foo', [], []));
142+
143+
$logs = Logs::getInstance()->aggregator()->all();
144+
$this->assertCount(1, $logs);
145+
$log = $logs[0];
146+
$this->assertArrayNotHasKey('channel', $log->attributes()->toSimpleArray());
147+
}
148+
149+
public function testChannelIncludedWhenEnabled(): void
150+
{
151+
$handler = new LogsHandler(LogLevel::warn(), true, true);
152+
$handler->handle(RecordFactory::create('with channel', Logger::WARNING, 'channel.foo', [], []));
153+
154+
$logs = Logs::getInstance()->aggregator()->all();
155+
$this->assertCount(1, $logs);
156+
$log = $logs[0];
157+
$this->assertArrayHasKey('channel', $log->attributes()->toSimpleArray());
158+
$this->assertSame('channel.foo', $log->attributes()->toSimpleArray()['channel']);
159+
}
160+
138161
public function testOriginTagNotAppliedWhenUsingDirectly()
139162
{
140163
\Sentry\logger()->info('No origin attribute');

0 commit comments

Comments
 (0)