Sentry Logging in Laravel

TJ Miller
2 min readJan 3, 2018

--

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

  • Its 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. Lets dive in…

Setup

app\Providers\LogServiceProvider.php

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.

Usage

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.

logger()->error('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.

Customization

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.

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

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, at International Bancard we use Graylog for a lot of other system logs. It wouldn’t be hard to modify this setup to use Graylog instead.

News Update!

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

--

--

TJ Miller

Fullstack code cowboy, ADHD enthusiast 🙃, currently hyper focused on LLM agent chains and related technology, flips knives for fun (balisong).