Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Monolog/LogsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public function isHandling($record): bool
*/
public function handle($record): bool
{
if (!$this->isHandling($record)) {
return false;
}
// Do not collect logs for exceptions, they should be handled seperately by the `Handler` or `captureException`
if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Throwable) {
return false;
Expand Down
110 changes: 107 additions & 3 deletions tests/Monolog/LogsHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

final class LogsHandlerTest extends TestCase
{
protected function setUp(): void
{
Logs::getInstance()->flush();
}

/**
* @dataProvider handleDataProvider
*/
Expand All @@ -36,9 +41,6 @@ public function testHandle($record, Log $expectedLog): void

$logs = Logs::getInstance()->aggregator()->all();

// Clear the logs aggregator to avoid side effects in other tests
Logs::getInstance()->aggregator()->flush();

$this->assertCount(1, $logs);

$log = $logs[0];
Expand All @@ -58,6 +60,28 @@ static function (string $key) {
);
}

/**
* @dataProvider logLevelDataProvider
*/
public function testLogLevels($record, int $countLogs): void
{
$client = ClientBuilder::create([
'enable_logs' => true,
'before_send' => static function () {
return null;
},
])->getClient();

$hub = new Hub($client);
SentrySdk::setCurrentHub($hub);

$handler = new LogsHandler(LogLevel::warn());
$handler->handle($record);

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

public static function handleDataProvider(): iterable
{
yield [
Expand Down Expand Up @@ -197,4 +221,84 @@ public static function handleDataProvider(): iterable
->setAttribute('bar', 'baz'),
];
}

public static function logLevelDataProvider(): iterable
{
yield [
RecordFactory::create(
'foo bar',
Logger::DEBUG,
'channel.foo',
[],
[]
),
0,
];

yield [
RecordFactory::create(
'foo bar',
Logger::NOTICE,
'channel.foo',
[],
[]
),
0,
];

yield [
RecordFactory::create(
'foo bar',
Logger::INFO,
'channel.foo',
[],
[]
),
0,
];

yield [
RecordFactory::create(
'foo bar',
Logger::WARNING,
'channel.foo',
[],
[]
),
1,
];

yield [
RecordFactory::create(
'foo bar',
Logger::CRITICAL,
'channel.foo',
[],
[]
),
1,
];

yield [
RecordFactory::create(
'foo bar',
Logger::ALERT,
'channel.foo',
[],
[]
),
1,
];

yield [
RecordFactory::create(
'foo bar',
Logger::EMERGENCY,
'channel.foo',
[],
[]
),
1,
];
}
}
Loading