From 688278d13479506f6e1aafb27a2927eb0730acbc Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Tue, 9 Sep 2025 09:05:37 +0200 Subject: [PATCH 1/2] Allow logging exceptions --- src/Sentry/Laravel/Logs/LogsHandler.php | 10 +++++++--- test/Sentry/Features/LogLogsIntegrationTest.php | 10 ++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Sentry/Laravel/Logs/LogsHandler.php b/src/Sentry/Laravel/Logs/LogsHandler.php index 41fd9d13..3972094a 100644 --- a/src/Sentry/Laravel/Logs/LogsHandler.php +++ b/src/Sentry/Laravel/Logs/LogsHandler.php @@ -10,6 +10,8 @@ use Sentry\Severity; use Throwable; +use function Sentry\logger; + class LogsHandler extends AbstractProcessingHandler { use CompatibilityProcessingHandlerTrait; @@ -95,20 +97,22 @@ public function getBatchFormatter(): FormatterInterface */ protected function doWrite($record): void { + $context = $record['context']; $exception = $record['context']['exception'] ?? null; if ($exception instanceof Throwable) { - return; + // Unset the exception object from the log context + unset($context['exception']); } - \Sentry\logger()->aggregator()->add( + logger()->aggregator()->add( // This seems a little bit of a roundabout way to get the log level, but this is done for compatibility self::getLogLevelFromSeverity( self::getSeverityFromLevel($record['level']) ), $record['message'], [], - array_merge($record['context'], $record['extra']) + array_merge($context, $record['extra']) ); } diff --git a/test/Sentry/Features/LogLogsIntegrationTest.php b/test/Sentry/Features/LogLogsIntegrationTest.php index 0569d1ae..352dc524 100644 --- a/test/Sentry/Features/LogLogsIntegrationTest.php +++ b/test/Sentry/Features/LogLogsIntegrationTest.php @@ -77,7 +77,7 @@ public function testLogChannelGeneratesLogsOnlyForConfiguredLevel(): void $this->assertEquals('Sentry Laravel error log message', $log->getBody()); } - public function testLogChannelDoesntCaptureExceptions(): void + public function testLogChannelCapturesExceptions(): void { $logger = Log::channel('sentry_logs'); @@ -85,7 +85,13 @@ public function testLogChannelDoesntCaptureExceptions(): void $logs = $this->getAndFlushCapturedLogs(); - $this->assertCount(0, $logs); + $this->assertCount(1, $logs); + + $log = $logs[0]; + + $this->assertEquals(LogLevel::error(), $log->getLevel()); + $this->assertEquals('Sentry Laravel error log message', $log->getBody()); + $this->assertNull($log->attributes()->get('exception')); } public function testLogChannelAddsContextAsAttributes(): void From 3ac8295394b085f10b3d20aeffaeb4d9dcbcbd24 Mon Sep 17 00:00:00 2001 From: Michi Hoffmann Date: Wed, 10 Sep 2025 10:36:04 +0200 Subject: [PATCH 2/2] Update src/Sentry/Laravel/Logs/LogsHandler.php Co-authored-by: Martin Linzmayer --- src/Sentry/Laravel/Logs/LogsHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Sentry/Laravel/Logs/LogsHandler.php b/src/Sentry/Laravel/Logs/LogsHandler.php index 3972094a..739b9a54 100644 --- a/src/Sentry/Laravel/Logs/LogsHandler.php +++ b/src/Sentry/Laravel/Logs/LogsHandler.php @@ -98,7 +98,7 @@ public function getBatchFormatter(): FormatterInterface protected function doWrite($record): void { $context = $record['context']; - $exception = $record['context']['exception'] ?? null; + $exception = $context['exception'] ?? null; if ($exception instanceof Throwable) { // Unset the exception object from the log context