Using laravel’s logging channels with sentry

smknstd
smknstd
Mar 13, 2019 · 3 min read
Image for post
Image for post
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

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',
[
'context' => $contextArray
]
);

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!

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

code16

About open source projects at CODE 16

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store