Laravel Exception & Error Tracking

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

On this page:

Installation

First, install the honeybadger-laravel package via composer:

bash
composer require honeybadger-io/honeybadger-laravel

Laravel Version Support

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

Laravel Version Honeybadger Laravel Version
8.x 3.2.x (current)
7.x 3.x
6.x 2.x
5.x 1.x

Note: This package uses Laravel’s package discovery to register the service provider and facade to the framework. If you are using an older version of Laravel or do not use package discovery, you may need to manually register those components.


Next, add Honeybadger reporting to app/Exceptions/Handler.php. In Laravel 8.5 and later, the default handler comes with a register() method; you should add the Honeybadger client within the reportable() callback.

php
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:

php
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:

bash
php artisan honeybadger:install project-api-key

The honeybadger:install command does three things:

  1. Adds HONEYBADGER_API_KEY to .env and .env.example
  2. Creates Honeybadger’s config/honeybadger.php configuration file
  3. Sends a test notification to your Honeybadger project

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

Note: The default config for Laravel won’t report errors to Honeybadger on local and testing environments. You can change that with the report_data option.

Identifying Users and Controller/Action

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.

Adding Context

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

Facade

php
Honeybadger::context('key', $value);

DI Resolution

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

Helper Resolution

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

Check-Ins

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.

Run a one-off check-in

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.

bash
php artisan honeybadger:checkin Jiy63Xw

Scheduled Command

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

php
app/Console/Kernel.php
protected function schedule(Schedule $schedule) { $schedule->command('honeybadger:checkin Jiy63Xw')->everyFiveMinutes(); }

After a Scheduled Command

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.

php
app/Console/Kernel.php
protected function schedule(Schedule $schedule) { $schedule->command(SendEmails::class)->daily() ->thenPingHoneybadger('Jiy63Xw'); }

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:

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

After a Successful Scheduled Command

You can use the thenPingHoneybadger($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.

php
app/Console/Kernel.php
protected function schedule(Schedule $schedule) { $schedule->command(SendEmails::class)->daily() ->pingHoneybadgerOnSuccess('Jiy63Xw', 'production'); }

Using Honeybadger as a Logger

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:

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

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

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

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

php
$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:

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