bugsnag/bugsnag
Bugsnag error monitoring and exception reporting for PHP. Automatically captures unhandled exceptions and crashes, supports reporting handled errors, and adds user/context data. Integrations for Laravel, Lumen, Symfony, Magento, WordPress, and more.
Install the Package
composer require bugsnag/bugsnag
Publish Configuration
php artisan vendor:publish --provider="Bugsnag\Bugsnag\BugsnagServiceProvider"
This generates a .env.bugsnag file. Add your API key:
BUGSNAG_API_KEY=your_api_key_here
Configure Laravel Integration
Add to config/bugsnag.php:
'laravel' => [
'enabled' => env('BUGSNAG_ENABLED', true),
'report_queries' => env('BUGSNAG_REPORT_QUERIES', false),
],
First Use Case: Auto-Reporting Exceptions Bugsnag automatically captures uncaught exceptions. Test it by throwing an exception in a route:
Route::get('/test-error', function() {
throw new \Exception("Test error for Bugsnag");
});
Automatic Exception Handling
Bugsnag integrates with Laravel’s exception handler (App\Exceptions\Handler). Extend it to customize reporting:
public function report(Throwable $exception)
{
if (config('bugsnag.laravel.enabled')) {
Bugsnag::notifyException($exception);
}
parent::report($exception);
}
Manual Error Reporting
Use Bugsnag::notify() for custom events or handled exceptions:
try {
// Risky operation
} catch (\Exception $e) {
Bugsnag::notifyException($e, [
'user' => ['id' => auth()->id(), 'email' => auth()->user()->email],
'context' => 'Payment processing',
]);
}
Middleware for User Context Attach user data globally via middleware:
public function handle($request, Closure $next)
{
Bugsnag::configure()->setUser([
'id' => auth()->id(),
'email' => auth()->user()->email,
'name' => auth()->user()->name,
]);
return $next($request);
}
Query Monitoring (Laravel-Specific)
Enable in config/bugsnag.php and use:
Bugsnag::notifyQuery('SELECT * FROM users', [
'duration' => 1500, // ms
'bindings' => ['user_id' => 123],
]);
Bugsnag\Bugsnag\Lumen\BugsnagServiceProvider.Bugsnag\Bugsnag\Symfony\BugsnagBundle.Bugsnag::notify() with structured data for non-exception events (e.g., API failures).local environments:
'enabled' => app()->environment(['staging', 'production']),
Sensitive Data Leakage
beforeNotify to scrub data:
Bugsnag::configure()->setBeforeNotify(function ($payload) {
unset($payload['request']['headers']['authorization']);
return $payload;
});
Duplicate Reports
notify() calls for the same exception (e.g., in middleware + exception handler).Bugsnag::notifyOnce() or guard with a flag:
if (!session()->has('bugsnag_reported')) {
Bugsnag::notify($exception);
session()->put('bugsnag_reported', true);
}
Performance Overhead
Bugsnag::notifyAsync($exception); // Non-blocking
Laravel Queue Conflicts
Bugsnag::configure()->setQueueConnection('database');
Bugsnag::notify() with debug: true to log payloads locally:
Bugsnag::notify($exception, [], ['debug' => true]);
.env.bugsnag is loaded (check config('bugsnag.api_key')).503 responses; adjust sendRate in config if needed:
'sendRate' => 1.0, // 1 error per second
Custom Error Classes
Override Bugsnag\Bugsnag\ExceptionHandler to handle domain-specific exceptions:
Bugsnag::configure()->setExceptionHandler(function ($exception) {
if ($exception instanceof \App\Exceptions\PaymentFailed) {
return new \Bugsnag\Bugsnag\Error($exception, [
'type' => 'payment_failure',
'amount' => $exception->amount,
]);
}
return null; // Let Bugsnag handle it
});
Breadcrumbs Add contextual events before errors:
Bugsnag::leaveBreadcrumb('User clicked', ['button' => 'submit']);
Release Tracking
Use Bugsnag::configure()->setReleaseStage() to track deployments:
Bugsnag::configure()->setReleaseStage('production');
Ignoring Errors Filter out known false positives (e.g., deprecation notices):
Bugsnag::configure()->setIgnoreClasses([
\DeprecatedFunctionalityException::class,
]);
How can I help you explore Laravel packages today?