Using laravel’s logging channels with sentry

smknstd
code16
Published in
3 min readMar 13, 2019
Photo by Keagan Henman on Unsplash

Logging is an important part of modern applications, as it provides a great and easy way to identify points of failure and unwanted behaviors. And for good reasons logging has become a first class citizen in recent versions of laravel.

New features

Laravel 5.6 was a major update on logging management and introduced two new powerful concepts:

  • channels : it lets you log and organize stuff in different places instead of one default logger
  • stacks : it allows logging to multiple locations simultaneously and provide greater customization and control

Laravel + sentry

Having log files, even well organized, is great but modern “error tracking” tools like Sentry are revolutionary. They let you monitor in real time, investigate exceptions online with lots of context and notify when necessary. It’s easy to misunderstand what these platforms are made for and sentry is definitely not a logging management platform. Sentry is primarily used for reporting application exceptions, but there’s often cases where you want to capture other issues with your code, and that’s perfectly fine !

Sentry has a great laravel package which works great out of the box. But when you report specific events in dedicated channels, default configuration won’t let you keep track of channels in sentry…

Keep track of channels

The good news is, there’s a way to make it work and keep an important feature of channels : being able to filter issues/logs grouped by channel. The package’s documentation has a pretty clear section about this.

Let’s dig into an example and say I’d like to report and group specific events related to payment in my code:

Log::channel('stripe')->warning(
"Couldn't capture: auth has expired",
[
'my_user_id' => $userId,
'my_transaction_id' => $transaction_id,
]
);

As you might know, laravel’s logging configuration lives in config/logging.php. As we’d like to report payment logs in both a dedicated file and to sentry, we’ll define our stripe channel as a stack (combining file and sentry). It’s really important to define a “name” attribute on the main channel. This is the one that will be used internally by sentry to tag each issue.

<?php

return
[

'channels' => [
...

'stripe' => [
'driver' => 'stack',
'channels' => ['stripe_file', 'sentry'],
'name' => 'stripe' //this is the key used by sentry!
],

'stripe_file' => [
'driver' => 'daily',
'path' => storage_path('logs/stripe.log'),
'level' => 'debug',
'days' => 30,
],

'sentry' => [
'driver' => 'sentry',
'level' => 'warning'
],
],

];

Keep calm and filter

Now, all sentry’s issues has now a logger attribute set with your channel’s name. Sentry’s own documentation doesn’t mention this, but it seems you can filter using “logger:stripe”. You can even make it a saved search.

Happy logging y’all!

Screenshot from stripe related issues filtered with the logger attribute on sentry’s interface

--

--