Skip to content

Laravel integration guide

Typical installation time: 5 minutes

Hi there! You’ve found Honeybadger’s guide to Laravel error and exception tracking. Once installed, Honeybadger will automatically report errors wherever they may happen:

  • During a web request
  • In a scheduled command
  • In a background task
  • When a process crashes

First, install the honeybadger-laravel package via composer:

Terminal window
composer require honeybadger-io/honeybadger-laravel

Install the version of our package based on the version of Laravel you are using:

Laravel VersionHoneybadger Laravel Version
12.x4.x (current)
11.x4.x (current)
10.x4.x (current)
9.x3.13.x
8.x3.2.x
7.x3.x
6.x2.x
5.x1.x


Next, add Honeybadger reporting to Laravel 11 and later by adding the following to bootstrap/app.php (https://laravel.com/docs/12.x/errors#reporting-exceptions):

bootstrap/app.php
->withExceptions(function (Exceptions $exceptions) {
$exceptions->report(static function (Throwable $e) {
if (app()->bound('honeybadger')) {
app('honeybadger')->notify($e, app('request'));
}
});
})

In Laravel 8.5 to 10, the default handler comes with a register() method; you should add the Honeybadger client within the reportable() callback.

app/Exceptions/Handler.php
public function register()
{
$this->reportable(function (Throwable $e) {
if (app()->bound('honeybadger')) {
app('honeybadger')->notify($e, app('request'));
}
});
}

On earlier versions of Laravel, add the client within the report($exception) method:

public function report(Throwable $exception)
{
if (app()->bound('honeybadger') && $this->shouldReport($exception)) {
app('honeybadger')->notify($exception, app('request'));
}
parent::report($exception);
}

Finally, run the honeybadger:install artisan command.

Terminal window
php artisan honeybadger:install [Your project API key]

If you are using our EU stack, add the --endpoint and the --appEndpoint flags to the honeybadger:install command:

Terminal window
php artisan honeybadger:install [Your project API key] --endpoint=https://eu-api.honeybadger.io --appEndpoint=https://eu-app.honeybadger.io

The honeybadger:install command does three things:

  1. Adds HONEYBADGER_API_KEY to .env and .env.example
  2. If you added the --endpoint and --appEndpoint flags, it also adds HONEBADGER_ENDPONT and HONEYBADGER_APP_ENDPOINT to .env and .env.example.
  3. Creates Honeybadger’s config/honeybadger.php configuration file
  4. Sends a test notification to your Honeybadger project

If everything is set up correctly, you should now have an error report in Honeybadger!

Honeybadger automatically captures details about the current logged-in user, as well as the controller and method name. No extra configuration needed. We only capture the user ID, so no sensitive information is transmitted.

When an error occurs, you’ll see an Affected Users section on your dashboard, where we’ll list the user IDs and how many times they encountered the error.

Context can be added by either the provided Facade or by resolving from the service container.

Honeybadger::context('key', $value);
use Honeybadger\Honeybadger;
public function __construct(Honeybadger $honeybadger)
{
$honeybadger->context('key', $value);
}
use Honeybadger\Honeybadger;
public function __construct()
{
app('honeybadger')->context('key', $value);
app(Honeybadger::class)->context('key', $value)
}

honeybadger-laravel integrates with Honeybadger’s Check-In feature to help you know when your scheduled tasks and background jobs go missing or silently fail.

To get started, create a new check-in in the Check-Ins tab of your project dashboard. You’ll be given a check-in URL that looks like this: https://api.honeybadger.io/v1/check_in/Jiy63Xw. Take note of the check-in ID; it’s the last part of the check-in URL. In this example, it’s Jiy63Xw. Alternatively, you can setup your check-ins entirely within your configuration file. If you follow this method, you don’t need the check-in ID anymore and instead you can use the check-in slug.

To run a one-off check-in, use the honeybadger:checkin command with your check-in ID. This will let Honeybadger know that your app is alive.

Terminal window
php artisan honeybadger:checkin Jiy63Xw

Or if you have configured your check-ins in your configuration file:

Terminal window
php artisan honeybadger:checkin "my-checkin"

You can schedule the check-in command to run at an interval. This method is great for ensuring your application is up and running.

app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('honeybadger:checkin Jiy63Xw')->everyFiveMinutes();
// or using the check-in slug
$schedule->command('honeybadger:checkin "my-checkin"')->everyFiveMinutes();
}

You can use the thenPingHoneybadger($checkInId) macro to check-in after certain scheduled commands are run. This method is great for making sure specific scheduled commands are running on time.

app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command(SendEmails::class)->daily()
->thenPingHoneybadger('Jiy63Xw');
// or using the check-in slug
->thenPingHoneybadger('my-checkin');
}

In this example, if SendEmails fails to run for some reason, Honeybadger will notify you.

You can also specify the environments where the check-in is allowed to run:

app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command(SendEmails::class)->daily()
->thenPingHoneybadger('Jiy63Xw', 'production');
// or using the check-in slug
->thenPingHoneybadger('my-checkin', 'production');
$schedule->command(CheckStatus::class)->daily()
->thenPingHoneybadger('Jiy63Xw', ['production', 'staging']);
// or using the check-in slug
->thenPingHoneybadger('my-checkin', ['production', 'staging']);
}

You can use the pingHoneybadgerOnSuccess($checkInId) macro to ensure that a certain command was run and completed successfully. This method is great for making sure specific scheduled commands are running on time only if it was successful. Like the thenPingHoneybadger method, you can also restrict it to specific environments.

app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command(SendEmails::class)->daily()
->pingHoneybadgerOnSuccess('Jiy63Xw', 'production');
// or using the check-in slug
->pingHoneybadgerOnSuccess('my-checkin', 'production');
}

If you prefer, you can also use Honeybadger as a log channel in your Laravel app. To do this, you’ll need to register a custom channel in your config/logging.php, making use of the HoneybadgerLogDriver:

config/logging.php
'channels' => [
// ...
'honeybadger' => [
'driver' => 'custom',
'via' => Honeybadger\HoneybadgerLaravel\HoneybadgerLogDriver::class,
'name' => 'honeybadger',
'level' => 'error',
],
],

Now you can write log messages as normal with Laravel’s log facade, and they’ll show up on your Honeybadger dashboard.

Log::channel('honeybadger')->error('An error message');
Log::channel('honeybadger')->error('An error message with context', ["some-key" => "some-value"]);
Log::channel('honeybadger')->error($exception);

If you include an exception context item in your error messages, we’ll automatically format them for easy viewing:

$e = new \Exception('Something happened');
Log::channel('honeybadger')->error('An error message', ['exception' => $e]);

You can also add the custom channel to your default stack so you can automatically have exceptions logged to Honeybadger as well:

config/logging.php
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single', 'honeybadger'],
'ignore_exceptions' => false,
],
// ...
],