Skip to content

Commit b6ba782

Browse files
cleptricLitarnus
andauthored
Allow logging exceptions (#1035)
Co-authored-by: Martin Linzmayer <[email protected]>
1 parent 2218b9c commit b6ba782

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/Sentry/Laravel/Logs/LogsHandler.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Sentry\Severity;
1111
use Throwable;
1212

13+
use function Sentry\logger;
14+
1315
class LogsHandler extends AbstractProcessingHandler
1416
{
1517
use CompatibilityProcessingHandlerTrait;
@@ -95,20 +97,22 @@ public function getBatchFormatter(): FormatterInterface
9597
*/
9698
protected function doWrite($record): void
9799
{
98-
$exception = $record['context']['exception'] ?? null;
100+
$context = $record['context'];
101+
$exception = $context['exception'] ?? null;
99102

100103
if ($exception instanceof Throwable) {
101-
return;
104+
// Unset the exception object from the log context
105+
unset($context['exception']);
102106
}
103107

104-
\Sentry\logger()->aggregator()->add(
108+
logger()->aggregator()->add(
105109
// This seems a little bit of a roundabout way to get the log level, but this is done for compatibility
106110
self::getLogLevelFromSeverity(
107111
self::getSeverityFromLevel($record['level'])
108112
),
109113
$record['message'],
110114
[],
111-
array_merge($record['context'], $record['extra'])
115+
array_merge($context, $record['extra'])
112116
);
113117
}
114118

test/Sentry/Features/LogLogsIntegrationTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,21 @@ public function testLogChannelGeneratesLogsOnlyForConfiguredLevel(): void
7777
$this->assertEquals('Sentry Laravel error log message', $log->getBody());
7878
}
7979

80-
public function testLogChannelDoesntCaptureExceptions(): void
80+
public function testLogChannelCapturesExceptions(): void
8181
{
8282
$logger = Log::channel('sentry_logs');
8383

8484
$logger->error('Sentry Laravel error log message', ['exception' => new \Exception('Test exception')]);
8585

8686
$logs = $this->getAndFlushCapturedLogs();
8787

88-
$this->assertCount(0, $logs);
88+
$this->assertCount(1, $logs);
89+
90+
$log = $logs[0];
91+
92+
$this->assertEquals(LogLevel::error(), $log->getLevel());
93+
$this->assertEquals('Sentry Laravel error log message', $log->getBody());
94+
$this->assertNull($log->attributes()->get('exception'));
8995
}
9096

9197
public function testLogChannelAddsContextAsAttributes(): void

0 commit comments

Comments
 (0)