Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/Sentry/Laravel/Logs/LogsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Sentry\Severity;
use Throwable;

use function Sentry\logger;

class LogsHandler extends AbstractProcessingHandler
{
use CompatibilityProcessingHandlerTrait;
Expand Down Expand Up @@ -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'])
);
}

Expand Down
10 changes: 8 additions & 2 deletions test/Sentry/Features/LogLogsIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,21 @@ 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');

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

$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
Expand Down