Installation:
composer require bugsnag/bugsnag-laravel
Publish the config file:
php artisan vendor:publish --provider="Bugsnag\BugsnagLaravel\BugsnagServiceProvider" --tag="config"
Configuration:
.env:
BUGSNAG_API_KEY=your_api_key_here
config/bugsnag.php with your project details (e.g., releaseStage, appVersion).First Use Case:
Route::get('/test-error', function() {
throw new \Exception("Test error for Bugsnag");
});
try {
// Risky operation
} catch (\Exception $e) {
Bugsnag::notify($e, [
'custom_data' => ['user_id' => auth()->id()]
]);
}
Automatic Exception Handling:
app/Exceptions/Handler.php). No manual setup required for unhandled exceptions.public function report(Throwable $exception)
{
if (app()->bound('bugsnag')) {
Bugsnag::notify($exception);
}
parent::report($exception);
}
Manual Error Reporting:
Bugsnag::notify() in services, jobs, or controllers for granular control.Bugsnag::notify($exception, [
'user' => auth()->user(),
'request' => request()->all(),
'custom_metadata' => ['feature_flag' => 'new_ui']
]);
Middleware for Global Context:
public function handle($request, Closure $next)
{
Bugsnag::leaveBreadcrumb('User Action', [
'user_id' => auth()->id(),
'action' => 'view_profile'
]);
return $next($request);
}
Lumen Integration:
bootstrap/app.php:
$app->register(\Bugsnag\BugsnagLaravel\BugsnagServiceProvider::class);
Bugsnag::notify() identically to Laravel.Testing:
Bugsnag in unit tests to verify error reporting:
$this->partialMock(Bugsnag::class, function ($mock) {
$mock->shouldReceive('notify')->once();
});
Sensitive Data Leakage:
request()->all()) or user passwords to reports. Use Bugsnag::notify() with filtered payloads:
Bugsnag::notify($e, [
'user' => auth()->user()->only('id', 'email')
]);
config/bugsnag.php to redact sensitive keys:
'redact_keys' => ['password', 'api_key', 'credit_card'],
Duplicate Reports:
releaseStage and appVersion in config to avoid fragmentation.Performance Overhead:
Bugsnag::leaveBreadcrumb()) can slow down requests. Use sparingly in production.Lumen Quirks:
Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables bootstrap step to avoid missing .env variables.Verify Reports:
releaseStage to filter test vs. production reports.Local Testing:
config/bugsnag.php during development:
'enabled' => env('APP_ENV') !== 'local',
Log Levels:
Bugsnag::notify($e, [], Bugsnag::SEVERITY_CRITICAL);
Custom Error Groups:
groupingHash:
Bugsnag::notify($e, [], null, null, 'unique_grouping_hash');
Event Listeners:
illuminate.auth.Event):
public function handle(Authenticated $event)
{
Bugsnag::leaveBreadcrumb('User Logged In', ['user_id' => $event->user->id]);
}
Webhook Integration:
Custom Error Classes:
Bugsnag\BugsnagLaravel\Bugsnag to add project-specific methods:
class CustomBugsnag extends Bugsnag
{
public function reportPaymentFailure($exception, $amount)
{
$this->notify($exception, [
'payment' => ['amount' => $amount, 'currency' => 'USD']
]);
}
}
How can I help you explore Laravel packages today?