sentry/sentry-laravel
Official Sentry SDK for Laravel. Automatically capture and report unhandled exceptions and performance data to Sentry, with seamless Laravel integration and configuration. Supports modern Laravel versions and includes tooling for monitoring errors in production.
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 in bootstrap/app.php:
use Sentry\Laravel\Integration;
return Application::configure(...)
->withExceptions(function (Exceptions $exceptions) {
Integration::handles($exceptions);
})
->create();
First Use Case: Capture an exception in a controller:
use function Sentry\captureException;
try {
$user = User::findOrFail($id);
} catch (\Throwable $e) {
captureException($e);
}
.env for SENTRY_LARAVEL_DSN and APP_ENV=production (or equivalent).Integration::handles() is in bootstrap/app.php.Exception Handling:
Integration::handles().try { ... } catch (\Throwable $e) {
captureException($e, [
'user' => auth()->user(),
'context' => ['key' => 'value']
]);
}
Log::channel('sentry')->error('Failed to process', ['data' => $data]);
Performance Monitoring:
use function Sentry\startTransaction;
$transaction = startTransaction('user.profile', 'task');
try {
$user = User::find($id);
$transaction->setStatus('ok');
} finally {
$transaction->finish();
}
use function Sentry\startSpan;
$span = startSpan('database.query');
DB::query(...);
$span->finish();
Logging:
Log::channel('sentry_logs')->info('User {id} logged in', ['id' => $user->id]);
.env:
LOG_CHANNEL=stack
LOG_STACK=single,sentry_logs
SENTRY_ENABLE_LOGS=true
Middleware Integration:
use Sentry\Laravel\Middleware\SentryTracesSampleRate;
protected $middleware = [
SentryTracesSampleRate::class,
];
User Context:
Sentry\Laravel\Integration:
// Manually override:
Sentry\configureScope(function ($scope) {
$scope->setUser(['id' => 123, 'email' => 'user@example.com']);
});
Sentry\resetTransactionName();
// Flags are automatically attached to events/spans.
Sentry\trace_metrics()->count('api.calls', 1, ['endpoint' => 'users']);
Sentry\trace_metrics()->gauge('memory.usage', memory_get_usage(), \Sentry\Unit::byte());
DSN Configuration:
APP_ENV=production may send events to a staging DSN.SENTRY_LARAVEL_DSN environment variable and validate in .env.example.Transaction Naming:
Sentry\resetTransactionName() in Octane middleware.Log Levels:
SENTRY_LOG_LEVEL overrides LOG_LEVEL for Sentry logs. Set explicitly in .env:
LOG_LEVEL=debug
SENTRY_LOG_LEVEL=warning
User Context:
email attributes in auth()->user() may break Sentry’s user scope.AppServiceProvider:
Sentry\configureScope(function ($scope) {
$user = auth()->user();
$scope->setUser([
'id' => $user->id,
'email' => (string) $user->email,
]);
});
Cache Span Names:
Structured Logs:
sentry_logs channel or set SENTRY_ENABLE_LOGS=true.config/logging.php and .env:
'sentry_logs' => ['driver' => 'sentry_logs', 'level' => env('SENTRY_LOG_LEVEL')],
SENTRY_ENABLE_LOGS=true
Events Not Appearing:
SENTRY_LARAVEL_DSN is correct and not empty.APP_ENV is not local (unless testing).config/sentry.php:
'debug' => env('APP_DEBUG', false),
Performance Overhead:
'enabled' => env('APP_ENV') !== 'local',
Sentry\setTraceSampleRate(0.1) to sample 10% of transactions.Headers in Requests:
sentry-trace and baggage headers may be escaped incorrectly.Custom Integrations:
Sentry\Laravel\Integration to add framework-specific hooks:
use Sentry\Laravel\Integration as BaseIntegration;
class CustomIntegration extends BaseIntegration {
public function register()
{
$this->registerEventListeners();
$this->registerMiddleware();
}
}
Before/After Hooks:
Sentry\beforeSendEvent and Sentry\beforeSendTransaction:
Sentry\beforeSendEvent(function ($event) {
$event['tags']['custom'] = 'value';
return $event;
});
Log Flushing:
log_flush_threshold in config/sentry.php to auto-flush logs:
'logs' => [
'flush_threshold' => 50, // Flush after 50 logs
],
OTLP Integration:
'integrations' => [
\Sentry\Integration\OTLPIntegration::class,
],
SENTRY_LOGS_LEVEL is deprecated; use SENTRY_LOG_LEVEL.SENTRY_TRACES_SAMPLE_RATE defaults to 1.0 (100% sampling).strict_trace_continuation in config/sentry.php to validate org_id in baggage headers:
'tracing' => [
'strict_trace_continuation' => true,
],
How can I help you explore Laravel packages today?