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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"require": {
"php": "^7.2 | ^8.0",
"illuminate/support": "^6.0 | ^7.0 | ^8.0 | ^9.0 | ^10.0 | ^11.0 | ^12.0 | ^13.0",
"sentry/sentry": "^4.22.0",
"sentry/sentry": "^4.23.0",
"symfony/psr-http-message-bridge": "^1.0 | ^2.0 | ^6.0 | ^7.0 | ^8.0",
"nyholm/psr7": "^1.0"
},
Expand Down
3 changes: 3 additions & 0 deletions config/sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
// @see: https://docs.sentry.io/platforms/php/guides/laravel/configuration/options/#enable_logs
'enable_logs' => env('SENTRY_ENABLE_LOGS', false),

// @see: https://docs.sentry.io/platforms/php/guides/laravel/configuration/options/#log_flush_threshold
'log_flush_threshold' => env('SENTRY_LOG_FLUSH_THRESHOLD') === null ? null : (int) env('SENTRY_LOG_FLUSH_THRESHOLD'),

// The minimum log level that will be sent to Sentry as logs using the `sentry_logs` logging channel
'logs_channel_level' => env('SENTRY_LOG_LEVEL', env('SENTRY_LOGS_LEVEL', env('LOG_LEVEL', 'debug'))),

Expand Down
48 changes: 48 additions & 0 deletions test/Sentry/Features/LogLogsIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Facades\Log;
use Orchestra\Testbench\Attributes\DefineEnvironment;
use Sentry\Laravel\Tests\TestCase;
use Sentry\EventType;
use Sentry\Logs\LogLevel;
use function Sentry\logger;

Expand Down Expand Up @@ -96,6 +97,53 @@ public function testLogChannelCapturesExceptions(): void
$this->assertNull($log->attributes()->get('exception'));
}

public function testLogChannelFlushesImmediatelyWhenThresholdIsReached(): void
{
$this->resetApplicationWithConfig([
'sentry.log_flush_threshold' => 2,
]);

$logger = Log::channel('sentry_logs');

$logger->warning('Sentry Laravel warning log message');
$logger->error('Sentry Laravel error log message');

$this->assertCount(0, logger()->aggregator()->all());

$logEvents = array_values(array_filter($this->getCapturedSentryEvents(), static function (array $event): bool {
return $event[0]->getType() === EventType::logs();
}));

$this->assertCount(1, $logEvents);
$this->assertCount(2, $logEvents[0][0]->getLogs());
$this->assertEquals('Sentry Laravel warning log message', $logEvents[0][0]->getLogs()[0]->getBody());
$this->assertEquals('Sentry Laravel error log message', $logEvents[0][0]->getLogs()[1]->getBody());
}

public function testLogChannelDoesNotFlushImmediatelyWhenThresholdIsNull(): void
{
$this->resetApplicationWithConfig([
'sentry.log_flush_threshold' => null,
]);

$logger = Log::channel('sentry_logs');

$logger->warning('Sentry Laravel warning log message');
$logger->error('Sentry Laravel error log message');

$bufferedLogs = logger()->aggregator()->all();

$this->assertCount(2, $bufferedLogs);

$logEvents = array_values(array_filter($this->getCapturedSentryEvents(), static function (array $event): bool {
return $event[0]->getType() === EventType::logs();
}));

$this->assertCount(0, $logEvents);

logger()->aggregator()->flush();
}

public function testLogChannelAddsContextAsAttributes(): void
{
$logger = Log::channel('sentry_logs');
Expand Down
18 changes: 18 additions & 0 deletions test/Sentry/Laravel/LaravelContainerConfigOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ public function testStrictTraceContinuationIsResolvedFromConfig(): void
$this->assertTrue($enabled);
}

public function testLogFlushThresholdIsNullByDefault(): void
{
$logFlushThreshold = app(HubInterface::class)->getClient()->getOptions()->getLogFlushThreshold();

$this->assertNull($logFlushThreshold);
}

public function testLogFlushThresholdIsResolvedFromConfig(): void
{
$this->resetApplicationWithConfig([
'sentry.log_flush_threshold' => 2,
]);

$logFlushThreshold = app(HubInterface::class)->getClient()->getOptions()->getLogFlushThreshold();

$this->assertSame(2, $logFlushThreshold);
}

public function testLoggerIsNullByDefault(): void
{
$logger = app(HubInterface::class)->getClient()->getOptions()->getLogger();
Expand Down
Loading