Installation
composer require airbrake/phpbrake
Add the service provider and alias to config/app.php:
'providers' => [
// ...
Airbrake\PHPBrake\ServiceProvider::class,
],
'aliases' => [
// ...
'Airbrake' => Airbrake\PHPBrake\Facades\Airbrake::class,
],
Configuration Publish the config file:
php artisan vendor:publish --provider="Airbrake\PHPBrake\ServiceProvider" --tag="config"
Update .env with your project ID and API key:
AIRBRAKE_PROJECT_ID=your_project_id
AIRBRAKE_API_KEY=your_api_key
First Error Capture Trigger an error to test:
Airbrake::notify('Test error', new \Exception('This is a test error'));
Verify the error appears in your Airbrake dashboard.
config/airbrake.php – Adjust environment, filters, and integrations.Airbrake::notify() – Primary method for error reporting.Airbrake\PHPBrake\Middleware\ReportExceptions – Auto-capture exceptions in Laravel.Add the middleware to app/Http/Kernel.php:
protected $middleware = [
// ...
\Airbrake\PHPBrake\Middleware\ReportExceptions::class,
];
try-catch blocks; all uncaught exceptions are reported.config/airbrake.php to filter them:
'ignore_status_codes' => [404, 403],
Useful for logging business logic errors or silent failures:
try {
$user = User::findOrFail($id);
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
Airbrake::notify('User not found', $e, [
'user_id' => $id,
'context' => 'API endpoint',
]);
}
ErrorHandlerExtend the default handler for pre-processing:
Airbrake::setErrorHandler(function (Throwable $exception, array $context = []) {
// Add custom logic (e.g., redact sensitive data)
$context['sanitized'] = true;
return Airbrake::notify('Custom handler', $exception, $context);
});
Airbrake::boot() in AppServiceProvider to auto-configure.ErrorListener or HttpKernel.$logger = new AirbrakeLogger(Airbrake::getClient());
$monolog->pushHandler($logger);
Use Laravel’s .env to toggle Airbrake per environment:
AIRBRAKE_ENABLED=true # Global toggle
AIRBRAKE_ENABLED_PRODUCTION=true # Override for production
In config/airbrake.php:
'enabled' => env('AIRBRAKE_ENABLED', false),
Performance Overhead
AIRBRAKE_ENABLED=false).if (rand(0, 100) < 10) { // 10% chance
Airbrake::notify('Batch job error', $e);
}
Sensitive Data Leakage
context. Use:
'user_id' => $user->id, // Safe
'api_key' => 'REDACTED', // Explicitly redact
config/airbrake.php to filter sensitive keys:
'filter_keys' => ['password', 'api_key', 'token'],
Duplicate Errors
Airbrake::notify("Failed to process order #{$order->id}", $e);
Middleware Conflicts
ReportExceptions, ensure it runs after Laravel’s App\Exceptions\Handler to avoid double-processing.Verify API Key/Project ID
Airbrake::notify('Test', new \Exception('Debug: Is this reaching Airbrake?'));
Check HTTP Status Codes
ignore_status_codes (e.g., 404). Adjust in config/airbrake.php if needed.Log Locally First
Log::error() alongside Airbrake::notify() during development:
Log::error('Local log', ['exception' => $e]);
Airbrake::notify('Remote log', $e);
Inspect the Client
$client = Airbrake::getClient();
$client->setEnvironment('staging'); // Override environment
Custom Notifiers
Airbrake\PHPBrake\NotifierInterface for non-HTTP errors (e.g., CLI jobs):
Airbrake::setNotifier(new CustomNotifier());
Webhook Integration
$webhookUrl = 'https://hooks.slack.com/...';
$payload = Airbrake::getClient()->getLastError();
Http::post($webhookUrl, $payload);
Error Grouping
error_id to group related errors (e.g., failed payments):
Airbrake::notify('Payment failed', $e, [
'error_id' => 'payment_' . $payment->id,
]);
Rate Limiting
if (!Airbrake::isRateLimited()) {
Airbrake::notify('Rate-limited error', $e);
}
How can I help you explore Laravel packages today?