A package to dump anything to the log
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.
What are your thoughts on "A package to dump anything to the log"?