diff --git a/src/Sentry/Laravel/Logs/LogsHandler.php b/src/Sentry/Laravel/Logs/LogsHandler.php index 41fd9d13..739b9a54 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 { - $exception = $record['context']['exception'] ?? null; + $context = $record['context']; + $exception = $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