chesscom/honeybadger-php
PHP integration for Honeybadger error monitoring and reporting. Capture exceptions, log errors, and send notices to Honeybadger from your app or framework with configurable API key, environment, and context for faster debugging.
Installation
composer require chesscom/honeybadger-php
Basic Configuration
Add your API key to .env:
HONEYBADGER_API_KEY=your_api_key_here
HONEYBADGER_ENVIRONMENT=production
First Integration
Register the middleware in app/Http/Kernel.php:
protected $middleware = [
\HoneyBadger\HoneyBadgerMiddleware::class,
];
Verify Setup
Trigger a test error (e.g., abort(500) in a route) and check HoneyBadger’s dashboard for the notification.
Exception Handling
Override Laravel’s report() method in App\Exceptions\Handler.php:
public function report(Throwable $exception)
{
if (!app()->bound('honeybadger')) return;
app('honeybadger')->notify($exception);
parent::report($exception);
}
Queue-Based Reporting Use Laravel queues to avoid blocking HTTP responses:
// In a service or controller
HoneyBadger::notify($exception)->queue();
Contextual Error Tracking Attach request/user data before notifying:
HoneyBadger::notify($exception)
->context(['user_id' => auth()->id(), 'request_data' => request()->all()]);
Artisan Command Errors Wrap command logic in a try-catch and notify HoneyBadger:
try {
// Command logic
} catch (\Exception $e) {
HoneyBadger::notify($e);
throw $e;
}
Middleware for API Errors Create a custom middleware to catch HTTP errors:
public function handle($request, Closure $next)
{
try {
return $next($request);
} catch (\Symfony\Component\HttpKernel\Exception\HttpException $e) {
HoneyBadger::notify($e);
throw $e;
}
}
database or redis queues for async error reporting.failed events.HoneyBadgerTestCase for unit/feature tests:
public function testErrorNotification()
{
HoneyBadger::notify(new \Exception('Test'));
$this->assertHoneyBadgerNotificationSent();
}
HONEYBADGER_API_KEY_STAGING=...
HONEYBADGER_API_KEY_PROD=...
API Key Exposure
.env to version control. Use Laravel’s env() helper or config files for sensitive data.config/honeybadger.php with encrypted values if needed.Rate Limiting
HoneyBadger::notify($e)->queue('honeybadger', ['attempts' => 3]);
Missing Context
HoneyBadger::notify($e)->context(['user' => auth()->user()]);
Performance Overhead
HoneyBadger::notify($e)->defer();
Laravel Version Mismatch
composer.json constraints or fork the package.Check API Responses
Enable debug mode in config/honeybadger.php:
'debug' => env('HONEYBADGER_DEBUG', false),
This logs API errors to Laravel’s log channel.
Mock HoneyBadger in Tests
Use the HoneyBadgerTestCase trait to verify notifications:
public function testErrorIsReported()
{
HoneyBadger::notify(new \Exception('Test'));
$this->assertHoneyBadgerNotificationSent();
}
Inspect Payloads Log the payload before sending:
HoneyBadger::setLogger(function ($payload) {
\Log::debug('HoneyBadger payload', ['payload' => $payload]);
});
Custom Error Groups
Override the group method in a service provider:
HoneyBadger::extend(function ($notifier) {
$notifier->group = function ($exception) {
return 'CustomGroup:' . $exception->getCode();
};
});
Filtering Errors
Ignore specific exceptions (e.g., ValidationException):
HoneyBadger::ignore(function ($exception) {
return $exception instanceof \Illuminate\Validation\ValidationException;
});
Custom Notifiers Add a notifier for third-party services (e.g., Stripe):
HoneyBadger::notify(new \Stripe\Exception\CardException('Test'))
->context(['service' => 'stripe']);
Webhook Integration Use HoneyBadger’s webhooks to trigger internal alerts (e.g., Slack):
// In a service provider
HoneyBadger::webhook('https://your-app.com/honeybadger-webhook');
Environment Variables
Prefix HoneyBadger config with HONEYBADGER_ in .env:
HONEYBADGER_API_KEY=...
HONEYBADGER_ENVIRONMENT=staging
HONEYBADGER_DEBUG=true
Fallback Behavior Configure a fallback logger if HoneyBadger fails:
HoneyBadger::setFallbackLogger(function ($exception) {
\Log::error('HoneyBadger failed', ['exception' => $exception]);
});
Payload Size Limits HoneyBadger may reject large payloads. Trim context data:
HoneyBadger::notify($e)
->context(array_slice($context, 0, 10)); // Limit to 10 keys
How can I help you explore Laravel packages today?