diff --git a/src/Monolog/LogsHandler.php b/src/Monolog/LogsHandler.php index 1d47fa90b..08fc55e74 100644 --- a/src/Monolog/LogsHandler.php +++ b/src/Monolog/LogsHandler.php @@ -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; diff --git a/tests/Monolog/LogsHandlerTest.php b/tests/Monolog/LogsHandlerTest.php index 4abda1919..9500fee59 100644 --- a/tests/Monolog/LogsHandlerTest.php +++ b/tests/Monolog/LogsHandlerTest.php @@ -16,6 +16,11 @@ final class LogsHandlerTest extends TestCase { + protected function setUp(): void + { + Logs::getInstance()->flush(); + } + /** * @dataProvider handleDataProvider */ @@ -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]; @@ -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 [ @@ -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, + ]; + } }