Skip to content

feat(monolog): add handler to only capture exceptions#2061

Open
Litarnus wants to merge 2 commits intomasterfrom
monolog-to-sentry-error-handler
Open

feat(monolog): add handler to only capture exceptions#2061
Litarnus wants to merge 2 commits intomasterfrom
monolog-to-sentry-error-handler

Conversation

@Litarnus
Copy link
Copy Markdown
Contributor

@Litarnus Litarnus commented Apr 9, 2026

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.

/**
* This Monolog handler will collect monolog events and send them to sentry.
*/
class SentryExceptionHandler extends AbstractHandler
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚲 : Not sure about the name, it's a bit redundant and generic so I'm up for suggestions

@Litarnus Litarnus marked this pull request as ready for review April 9, 2026 12:26
@Litarnus Litarnus linked an issue Apr 9, 2026 that may be closed by this pull request
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

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 in handle(), restoring Monolog 2 preflight compatibility.

Create PR

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add handler to send Monolog exceptions to Sentry

1 participant