Skip to content

Commit ab2c6c8

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 d94ee1b commit ab2c6c8

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/Monolog/LogsHandler.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,23 @@ class LogsHandler implements HandlerInterface
2929
*/
3030
private $bubble;
3131

32+
/**
33+
* Whether to include the channel name as an attribute in the Sentry logs.
34+
*/
35+
private bool $includeChannel;
36+
3237
/**
3338
* Creates a new Monolog handler that converts Monolog logs to Sentry logs.
3439
*
3540
* @param LogLevel|\Monolog\Level|int|null $logLevel the minimum logging level at which this handler will be triggered and collects the logs
3641
* @param bool $bubble whether the messages that are handled can bubble up the stack or not
42+
* @param bool $includeChannel whether to include the channel name as an attribute in the Sentry logs
3743
*/
38-
public function __construct($logLevel = null, bool $bubble = true)
44+
public function __construct($logLevel = null, bool $bubble = true, bool $includeChannel = false)
3945
{
4046
$this->logLevel = $logLevel ?? LogLevel::debug();
4147
$this->bubble = $bubble;
48+
$this->includeChannel = $includeChannel;
4249
}
4350

4451
/**
@@ -137,6 +144,11 @@ public function __destruct()
137144
*/
138145
protected function compileAttributes($record): array
139146
{
140-
return array_merge($record['context'], $record['extra'], ['sentry.origin' => 'auto.log.monolog']);
147+
return array_merge(
148+
$this->includeChannel ? ['channel' => $record['channel']] : [],
149+
$record['context'],
150+
$record['extra'],
151+
['sentry.origin' => 'auto.log.monolog']
152+
);
141153
}
142154
}

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)