-
-
Notifications
You must be signed in to change notification settings - Fork 464
ref(logs): allow different log level types #1992
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Litarnus
wants to merge
6
commits into
master
Choose a base branch
from
log-levels-compatibility
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+157
−97
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,19 +4,17 @@ | |
|
|
||
| namespace Sentry\Tests\Monolog; | ||
|
|
||
| use Monolog\Level; | ||
| use Monolog\Logger; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Sentry\ClientBuilder; | ||
| use Sentry\Event; | ||
| use Sentry\Logs\Log; | ||
| use Sentry\Logs\LogLevel; | ||
| use Sentry\Logs\Logs; | ||
| use Sentry\Monolog\LogsHandler; | ||
| use Sentry\SentrySdk; | ||
| use Sentry\State\Hub; | ||
| use Sentry\Transport\Result; | ||
| use Sentry\Transport\ResultStatus; | ||
| use Sentry\Transport\TransportInterface; | ||
| use Sentry\Tests\StubTransport; | ||
|
|
||
| final class LogsHandlerTest extends TestCase | ||
| { | ||
|
|
@@ -64,42 +62,47 @@ static function (string $key) { | |
| } | ||
|
|
||
| /** | ||
| * @dataProvider logLevelDataProvider | ||
| * @dataProvider monologLegacyLevelDataProvider | ||
| */ | ||
| public function testLogLevels($record, int $countLogs): void | ||
| public function testFiltersAndMapsUsingLegacyMonologThreshold(int $threshold, int $recordLevel, int $expectedCount, ?LogLevel $expectedMappedLevel): void | ||
| { | ||
| $handler = new LogsHandler(LogLevel::warn()); | ||
| $handler->handle($record); | ||
| $handler = new LogsHandler($threshold); | ||
| $handler->handle(RecordFactory::create('foo bar', $recordLevel, 'channel.foo', [], [])); | ||
|
|
||
| $logs = Logs::getInstance()->aggregator()->all(); | ||
| $this->assertCount($countLogs, $logs); | ||
| $this->assertCount($expectedCount, $logs); | ||
|
|
||
| if ($expectedMappedLevel !== null) { | ||
| $this->assertEquals($expectedMappedLevel, $logs[0]->getLevel()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider monologLevelDataProvider | ||
| */ | ||
| public function testFiltersAndMapsUsingMonologEnumThreshold($threshold, $recordLevel, int $expectedCount, ?LogLevel $expectedMappedLevel): void | ||
| { | ||
| if (!class_exists(Level::class)) { | ||
| $this->markTestSkipped('Test only works for Monolog >= 3'); | ||
| } | ||
|
|
||
| $this->assertInstanceOf(Level::class, $threshold); | ||
| $this->assertInstanceOf(Level::class, $recordLevel); | ||
|
|
||
| $handler = new LogsHandler($threshold); | ||
| $handler->handle(RecordFactory::create('foo bar', $recordLevel->value, 'channel.foo', [], [])); | ||
|
|
||
| $logs = Logs::getInstance()->aggregator()->all(); | ||
| $this->assertCount($expectedCount, $logs); | ||
|
|
||
| if ($expectedMappedLevel !== null) { | ||
| $this->assertEquals($expectedMappedLevel, $logs[0]->getLevel()); | ||
| } | ||
| } | ||
|
|
||
| public function testLogsHandlerDestructor() | ||
| { | ||
| $transport = new class implements TransportInterface { | ||
| private $events = []; | ||
|
|
||
| public function send(Event $event): Result | ||
| { | ||
| $this->events[] = $event; | ||
|
|
||
| return new Result(ResultStatus::success()); | ||
| } | ||
|
|
||
| public function close(?int $timeout = null): Result | ||
| { | ||
| return new Result(ResultStatus::success()); | ||
| } | ||
|
|
||
| /** | ||
| * @return Event[] | ||
| */ | ||
| public function getEvents(): array | ||
| { | ||
| return $this->events; | ||
| } | ||
| }; | ||
| $transport = new StubTransport(); | ||
| $client = ClientBuilder::create([ | ||
| 'enable_logs' => true, | ||
| ])->setTransport($transport) | ||
|
|
@@ -110,8 +113,8 @@ public function getEvents(): array | |
|
|
||
| $this->handleLogAndDrop(); | ||
|
|
||
| $this->assertCount(1, $transport->getEvents()); | ||
| $this->assertSame('I was dropped :(', $transport->getEvents()[0]->getLogs()[0]->getBody()); | ||
| $this->assertCount(1, StubTransport::$events); | ||
| $this->assertSame('I was dropped :(', StubTransport::$events[0]->getLogs()[0]->getBody()); | ||
| } | ||
|
|
||
| private function handleLogAndDrop(): void | ||
|
|
@@ -283,83 +286,127 @@ public static function handleDataProvider(): iterable | |
| ]; | ||
| } | ||
|
|
||
| public static function logLevelDataProvider(): iterable | ||
| public static function monologLegacyLevelDataProvider(): iterable | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests are maybe a bit too verbose and confusing, not sure |
||
| { | ||
| yield [ | ||
| RecordFactory::create( | ||
| 'foo bar', | ||
| Logger::DEBUG, | ||
| 'channel.foo', | ||
| [], | ||
| [] | ||
| ), | ||
| yield 'NOTICE threshold drops INFO (both map to sentry info)' => [ | ||
| Logger::NOTICE, | ||
| Logger::INFO, | ||
| 0, | ||
| null, | ||
| ]; | ||
|
|
||
| yield [ | ||
| RecordFactory::create( | ||
| 'foo bar', | ||
| Logger::NOTICE, | ||
| 'channel.foo', | ||
| [], | ||
| [] | ||
| ), | ||
| yield 'NOTICE threshold keeps NOTICE (mapped to sentry info)' => [ | ||
| Logger::NOTICE, | ||
| Logger::NOTICE, | ||
| 1, | ||
| LogLevel::info(), | ||
| ]; | ||
|
|
||
| yield 'NOTICE threshold keeps WARNING (mapped to sentry warn)' => [ | ||
| Logger::NOTICE, | ||
| Logger::WARNING, | ||
| 1, | ||
| LogLevel::warn(), | ||
| ]; | ||
|
|
||
| yield 'ALERT threshold drops CRITICAL (both map to sentry fatal)' => [ | ||
| Logger::ALERT, | ||
| Logger::CRITICAL, | ||
| 0, | ||
| null, | ||
| ]; | ||
|
|
||
| yield [ | ||
| RecordFactory::create( | ||
| 'foo bar', | ||
| Logger::INFO, | ||
| 'channel.foo', | ||
| [], | ||
| [] | ||
| ), | ||
| yield 'ALERT threshold keeps ALERT (mapped to sentry fatal)' => [ | ||
| Logger::ALERT, | ||
| Logger::ALERT, | ||
| 1, | ||
| LogLevel::fatal(), | ||
| ]; | ||
|
|
||
| yield 'ALERT threshold keeps EMERGENCY (mapped to sentry fatal)' => [ | ||
| Logger::ALERT, | ||
| Logger::EMERGENCY, | ||
| 1, | ||
| LogLevel::fatal(), | ||
| ]; | ||
|
|
||
| yield 'EMERGENCY threshold drops ALERT (both map to sentry fatal)' => [ | ||
| Logger::EMERGENCY, | ||
| Logger::ALERT, | ||
| 0, | ||
| null, | ||
| ]; | ||
|
|
||
| yield [ | ||
| RecordFactory::create( | ||
| 'foo bar', | ||
| Logger::WARNING, | ||
| 'channel.foo', | ||
| [], | ||
| [] | ||
| ), | ||
| yield 'EMERGENCY threshold keeps EMERGENCY (mapped to sentry fatal)' => [ | ||
| Logger::EMERGENCY, | ||
| Logger::EMERGENCY, | ||
| 1, | ||
| LogLevel::fatal(), | ||
| ]; | ||
| } | ||
|
|
||
| yield [ | ||
| RecordFactory::create( | ||
| 'foo bar', | ||
| Logger::CRITICAL, | ||
| 'channel.foo', | ||
| [], | ||
| [] | ||
| ), | ||
| public static function monologLevelDataProvider(): iterable | ||
| { | ||
| if (!class_exists(Level::class)) { | ||
| yield 'Monolog < 3 (skipped)' => [null, null, 0, null]; | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| yield 'NOTICE threshold drops INFO (both map to sentry info)' => [ | ||
| Level::Notice, | ||
| Level::Info, | ||
| 0, | ||
| null, | ||
| ]; | ||
|
|
||
| yield 'NOTICE threshold keeps NOTICE (mapped to sentry info)' => [ | ||
| Level::Notice, | ||
| Level::Notice, | ||
| 1, | ||
| LogLevel::info(), | ||
| ]; | ||
|
|
||
| yield [ | ||
| RecordFactory::create( | ||
| 'foo bar', | ||
| Logger::ALERT, | ||
| 'channel.foo', | ||
| [], | ||
| [] | ||
| ), | ||
| yield 'NOTICE threshold keeps WARNING (mapped to sentry warn)' => [ | ||
| Level::Notice, | ||
| Level::Warning, | ||
| 1, | ||
| LogLevel::warn(), | ||
| ]; | ||
|
|
||
| yield [ | ||
| RecordFactory::create( | ||
| 'foo bar', | ||
| Logger::EMERGENCY, | ||
| 'channel.foo', | ||
| [], | ||
| [] | ||
| ), | ||
| yield 'ALERT threshold drops CRITICAL (both map to sentry fatal)' => [ | ||
| Level::Alert, | ||
| Level::Critical, | ||
| 0, | ||
| null, | ||
| ]; | ||
|
|
||
| yield 'ALERT threshold keeps ALERT (mapped to sentry fatal)' => [ | ||
| Level::Alert, | ||
| Level::Alert, | ||
| 1, | ||
| LogLevel::fatal(), | ||
| ]; | ||
|
|
||
| yield 'ALERT threshold keeps EMERGENCY (mapped to sentry fatal)' => [ | ||
| Level::Alert, | ||
| Level::Emergency, | ||
| 1, | ||
| LogLevel::fatal(), | ||
| ]; | ||
|
|
||
| yield 'EMERGENCY threshold drops ALERT (both map to sentry fatal)' => [ | ||
| Level::Emergency, | ||
| Level::Alert, | ||
| 0, | ||
| null, | ||
| ]; | ||
|
|
||
| yield 'EMERGENCY threshold keeps EMERGENCY (mapped to sentry fatal)' => [ | ||
| Level::Emergency, | ||
| Level::Emergency, | ||
| 1, | ||
| LogLevel::fatal(), | ||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enum-to-int comparison fails in Monolog 3
High Severity
In Monolog 3,
$record['level']returns a\Monolog\Levelenum object, not an integer. The comparisons on lines 59 and 61 attempt to compare this enum directly with integer values ($this->logLevel->valueor$this->logLevel), which throws a TypeError in PHP 8.1+ since backed enums cannot be compared with integers using>=. TheBreadcrumbHandlerin the same codebase demonstrates the correct pattern: checkingif ($level instanceof Level)and extracting$level->valuebefore comparison.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The analysis seems to be incorrect: https://github.com/Seldaek/monolog/blob/main/src/Monolog/LogRecord.php#L86
levelis returning$this->level->value, which is the integer defined in the enum