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…
Setup
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.
“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.
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.
Updates
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)