skip to content
TJ Miller

Sentry Logging in Laravel

/ 2 min read

{.alert-info} Laravel 5.6 also introduced much better logging solutions called “channels”. Take a look at the docs.

There are a few reasons why I just hooked Sentry into the Laravel logging on my current project:

  • It’s hosted using Docker (highly available, multiple instances) and I need a central place to capture application logs
  • There are instances where I log messages that aren’t necessarily exceptions and want visibility
  • I’m already using Sentry for exception capturing

This was pretty easy to hook up. Let’s dive in…


namespace App\Providers;
use Monolog\Handler\RavenHandler;
use Illuminate\Support\Facades\Log;
use Monolog\Formatter\LineFormatter;
use Illuminate\Support\ServiceProvider;
class LogServiceProvider extends ServiceProvider
* Bootstrap any application services.
* @return void
public function boot()
if (app()->bound('sentry') && config('sentry.dsn') !== null) {
$handler = new RavenHandler(app('sentry'));
$handler->setFormatter(new LineFormatter("%message%\n"));
$monolog = Log::getMonolog();

Note: You can add some additional fields to the LineFormatter. See the “Customization” section below.

Then you need to add the provider to your config/app.php file:

return [
'providers' => [
* Application Service Providers...
// App\Providers\BroadcastServiceProvider::class,


Now anything that gets logged will be forwarded to Sentry. For example, you could use something like the following in your application to explicitly create a log entry.

'Unknown foo returned from the example service',
'foo' => 'bar',
'user_id' => auth()->user()->getAuthIdentifier()

“Unknown foo returned from the example service” will become the title of the entry on the Sentry project and you can find the context that you passed toward the bottom of the UI.

LineFormatter("%message% %context% %extra%\n"))


The example in the Sentry docs passes the context and extras but I prefer to just send the message as this will become the title of the entry on Sentry.


More on Monolog, the underlying log engine for Laravel, integration in the Sentry docs.

Beyond Sentry

You could also use this pattern for other log services too, for example, Graylog. It wouldn’t be hard to modify this setup to use Graylog instead.


After I published this article I got this response from the Founder/CEO of Sentry. Really excited to hear this!

we’re going to make this kind of use case even better this year — stay tuned! – David Cramer (@zeeg)


For more thoughts, links, project and personal updates.