Log Handler Bubbling in Laravel 5.6

Tom Ellis
2 min readMay 25, 2018

From Laravel 5.6 you could configure multiple log handlers, which is great. But there was one thing I noticed you can do with Monolog that wasn’t in Laravel’s logging documentation. And thats handler bubbling.

What is Log Handler Bubbling?

Handler bubbling allows you to control if the logger should continue going through the remaining handlers and checking if it can log to them, based on the log level.

How to setup Log Handler Bubbling

Say you want to split your logging out so all debug & info levels go to an audit.log and notice and above (so emergency, alert, critical, error, warning) would go to laravel.log.

So we need to add an new audit channel and then change the single channel level to notice and add the bubbling setting. Our config should now look something like this:

'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single', 'audit'],
],

'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'notice',
'bubble' => false,
],

'audit' => [
'driver' => 'single',
'path' => storage_path('logs/audit.log'),
'level' => 'debug',
],
],

So if the log type is notice or above (emergency, alert, critical, error, warning) it would use the single channel and because bubbling is set to false it would stop going through the handlers. Otherwise if it is debug or info it would use the audit channel.

FYI you can call the channel whatever you want, it doesn’t have to be audit. Now on the stack channel the channels need to be in a specific order. If the order was audit, single then the bubbling would have no affect and all errors would go to both log files.

Why would I need to do this?

Error monitoring. Whether your going through the log files manually or using Cloudwatch on AWS (I am) you want to make your life easy as possible. Even if your using New Relic/Bugsnag to monitor your PHP applications you may still need to refer to the log files for more information.

Conclusion

Little hidden (not yet documented) gems like this will make using Laravel a better experience. Who knows what else you can do thats not yet in the docs.

--

--

Tom Ellis

PHP and JavaScript hacker. Symfony and Laravel tinkerer. Open source developer.