feat(monolog): add handler to only capture exceptions#2061
Open
feat(monolog): add handler to only capture exceptions#2061
Conversation
Litarnus
commented
Apr 9, 2026
| /** | ||
| * This Monolog handler will collect monolog events and send them to sentry. | ||
| */ | ||
| class SentryExceptionHandler extends AbstractHandler |
Contributor
Author
There was a problem hiding this comment.
🚲 : Not sure about the name, it's a bit redundant and generic so I'm up for suggestions
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix prepared a fix for the issue found in the latest run.
- ✅ Fixed: Handler silently broken on Monolog 2 due to isHandling
isHandling()now delegates only to the parent level check and exception presence is enforced inhandle(), restoring Monolog 2 preflight compatibility.
Or push these changes by commenting:
@cursor push 5f3ed9ddf8
Preview (5f3ed9ddf8)
diff --git a/src/Monolog/SentryExceptionHandler.php b/src/Monolog/SentryExceptionHandler.php
--- a/src/Monolog/SentryExceptionHandler.php
+++ b/src/Monolog/SentryExceptionHandler.php
@@ -37,10 +37,6 @@
*/
public function isHandling($record): bool
{
- if ($this->getExceptionFromRecord($record) === null) {
- return false;
- }
-
/** @var LogRecord $record */
return parent::isHandling($record);
}
diff --git a/tests/Monolog/SentryExceptionHandlerTest.php b/tests/Monolog/SentryExceptionHandlerTest.php
--- a/tests/Monolog/SentryExceptionHandlerTest.php
+++ b/tests/Monolog/SentryExceptionHandlerTest.php
@@ -111,10 +111,24 @@
$handler = new SentryExceptionHandler(new Hub($client, new Scope()), Logger::DEBUG, false);
- $this->assertFalse($handler->isHandling($record));
+ $this->assertTrue($handler->isHandling($record));
$this->assertFalse($handler->handle($record));
}
+ public function testIsHandlingInMonolog2SupportsMinimalLevelRecord(): void
+ {
+ if (Logger::API >= 3) {
+ $this->markTestSkipped('Monolog 3 always passes full records to isHandling.');
+ }
+
+ /** @var ClientInterface&MockObject $client */
+ $client = $this->createMock(ClientInterface::class);
+ $handler = new SentryExceptionHandler(new Hub($client, new Scope()), Logger::WARNING, false);
+
+ $this->assertTrue($handler->isHandling(['level' => Logger::ERROR]));
+ $this->assertFalse($handler->isHandling(['level' => Logger::INFO]));
+ }
+
public function testHandleIgnoresRecordsBelowThreshold(): void
{
$exception = new \RuntimeException('boom');This Bugbot Autofix run was free. To enable autofix for future PRs, go to the Cursor dashboard.
Reviewed by Cursor Bugbot for commit a6a2a91. Configure here.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.


Adds a Monolog handler that will report exceptions to Sentry. In contrast to the
Handler, it will not convert log messages with a certain level to errors.By default it will report exceptions for all levels but it's possible to also restrict it to certain levels.