sentry/sentry-laravel
Official Sentry SDK for Laravel. Automatically captures unhandled exceptions, performance data, and context from your app, sending issues and traces to Sentry for faster debugging and monitoring. Supports modern Laravel versions with simple Composer install.
Installation:
composer require sentry/sentry-laravel
Configure DSN:
php artisan sentry:publish --dsn=YOUR_DSN_HERE
This generates config/sentry.php and adds SENTRY_LARAVEL_DSN to .env.
Enable Exception Handling (Laravel 11/12):
Update bootstrap/app.php:
->withExceptions(function (Exceptions $exceptions) {
\Sentry\Laravel\Integration::handles($exceptions);
})
First Use Case: Capture an exception in a controller:
use function Sentry\captureException;
try {
// Risky operation
} catch (\Throwable $e) {
captureException($e);
}
config/sentry.php for:
dsn (required)breadcrumbs (enable/disable)tracing (enable performance monitoring)logs (enable structured logging)SENTRY_TRACES_SAMPLE_RATE (default: 1.0 for 100% sampling)SENTRY_ENVIRONMENT (e.g., production, staging)Exception Handling:
Integration::handles().// For exceptions
captureException($e);
// For messages
captureMessage("User {$user->id} failed to checkout");
Performance Monitoring:
use function Sentry\startTransaction;
$transaction = startTransaction('checkout.process', 'checkout');
try {
// Business logic
} finally {
$transaction->finish();
}
SentryTransactionMiddleware for automatic route transactions:
// In app/Http/Kernel.php
protected $middlewareGroups = [
'web' => [
// ...
\Sentry\Laravel\Middleware\SentryTransactionMiddleware::class,
],
];
Structured Logging:
sentry_logs channel in config/logging.php:
'sentry_logs' => [
'driver' => 'sentry_logs',
'level' => env('SENTRY_LOG_LEVEL', 'info'),
],
\Log::info('Order processed', [
'order_id' => $order->id,
'user_id' => auth()->id(),
]);
User Context:
// In AppServiceProvider boot()
\Sentry\configureScope(function (\Sentry\State\Scope $scope) {
$scope->setUser([
'id' => auth()->id(),
'email' => auth()->user()->email,
]);
});
Custom Integrations:
config/sentry.php:
'integrations' => [
\Sentry\Laravel\Integration\Database::class,
],
Feature Flags:
// Flags are automatically attached to events/spans
if (feature('new_ui')->isEnabled()) {
// ...
}
// For jobs
\Sentry\Laravel\Middleware\SentryTracesSampleRate::class => ['rate' => 0.1],
\Sentry\trace_metrics()->gauge('api_latency', $durationMs, [], \Sentry\Unit::millisecond());
'integrations' => [
\Sentry\Laravel\Integration\OTLPIntegration::class,
],
DSN Configuration:
SENTRY_LARAVEL_DSN in .env or using a private DSN in production.php artisan sentry:publish and verify the DSN in .env.Performance Overhead:
tracing without sampling (SENTRY_TRACES_SAMPLE_RATE < 1.0) can bloat your Sentry quota.0.1 (10% sampling) and adjust based on volume.Structured Logging:
SENTRY_ENABLE_LOGS is missing or LOG_CHANNEL isn’t set to stack with sentry_logs.LOG_CHANNEL=stack
LOG_STACK=single,sentry_logs
SENTRY_ENABLE_LOGS=true
User Context:
email fields (e.g., Carbon instances) may break Sentry’s user scope.$scope->setUser([
'email' => (string) auth()->user()->email,
]);
Cache Instrumentation:
Transactions in Octane:
Environment Variables:
SENTRY_LOGS_LEVEL is deprecated; use SENTRY_LOG_LEVEL..env and config to use the new variable.if (!\Sentry\getClient()) {
throw new \RuntimeException("Sentry client not initialized!");
}
\Sentry\configureScope(function (\Sentry\State\Scope $scope) {
$scope->setDebug(true);
});
log_flush_threshold is set (default: 50).config/sentry.php or set SENTRY_LOG_FLUSH_THRESHOLD=10.Custom Integrations:
\Sentry\Integration to add custom instrumentation (e.g., for third-party services).class MyServiceIntegration extends \Sentry\Integration
{
public function __construct() {
$this->name = 'my-service';
}
public function setupOnce(): void {
// Hook into Laravel events or services
}
}
config/sentry.php:
'integrations' => [
\App\Integrations\MyServiceIntegration::class,
],
Scope Modifiers:
\Sentry\configureScope(function (\Sentry\State\Scope $scope) {
$scope->setTag('custom_tag', 'value');
$scope->setExtra('dynamic_data', ['key' => 'value']);
});
Before/After Hooks:
Sentry\beforeCapture and Sentry\afterCapture to modify events:
\Sentry\beforeCapture(function (\Sentry\Event $event) {
$event->setLevel(\Sentry\Severity::warning());
});
Testing:
\Sentry\init(['dsn' => '___MOCK_DSN___']);
\Sentry\setCaptureExceptionCallback(function () {
// Custom logic for tests
});
Tracing Sample Rate:
.env:
SENTRY_TRACES_SAMPLE_RATE=0.5
\Sentry\Laravel\Middleware\SentryTracesSampleRate::class => ['rate' => 0.2],
Strict Trace Continuation:
How can I help you explore Laravel packages today?