A package to dump anything to the log original

by Freek Van der Herten – 2 minute read

In this post, I'd like to show you a quick demo of our new package called laravel-log-dumper.

In Laravel, you can write stuff to the log using the facade or the info function.

Log::info('The meaning of life is 42.');

info("Don't panic.")

In some cases I find this easier to just dump things to the log than using the dump function.

But unfortunately, these functions don't take any input. Log::info will break when you give it an object, and info will break when passing an array to it.

// boom 💣
info([
    'Beware of the Leopard.',
    'Time is an illusion. Lunchtime doubly so.',
])

And you can't log multiple things at once.

// boom 💣
Log::info('In the beginning the universe was created.',  'This has made a lot of people very angry and been widely regarded as a bad move.');

To fix this, I've created a small package called spatie/laravel-log-dumper. It contains a function ld (short for log dumper) that can dump anything to the log. Strings, arrays, objects, ... No problem!

// a string
ld('He hoped and prayed that there wasn’t an afterlife.');

// an array
ld([
  'Then he realized there was a contradiction involved here',
  'and merely hoped that there wasn’t an afterlife.',
]);

You can also pass it as many arguments as you'd like. They will all get written to the log.

ld('a string', ['a', 'b', 'c'], $object);

Behind the scenes, Symfony's Vardumper is used to transform anything into a string.

It's a tiny package, but it can come in handy.

Join 9,500+ smart developers

Get my monthly newsletter with what I learn from running Spatie, building Oh Dear, and maintaining 300+ open source packages. Practical takes on Laravel, PHP, and AI that you can actually use.

"Freek publishes a super resourceful and practical newsletter. A must for anyone in the Laravel space"

Joey Kudish — Shipping with AI as a teammate

No spam. Unsubscribe anytime. You can also follow me on X.

Found something interesting to share? Submit a link to the community section.