Scout APM is PHP application performance monitoring designed for developers. With tracing logic that ties issues back to the line of code causing them, you can pinpoint n+1 queries, memory leaks, and other abnormalities in real time so you can knock them out and get back to building a great product. Start your free 14-day trial today and get the performance insight you need in less than 4 minutes.

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.

Stay up to date with all things Laravel, PHP, and JavaScript.

Follow me on Twitter. I regularly tweet out programming tips, and what I myself have learned in ongoing projects.

Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

Webmentions

Gaurav Makhecha replied on 18th April 2020
5/ @freekmurze released a new package to dump anything to the log - string/array/object.. pass any number of and any type of params.๐Ÿช€๐Ÿ—ผ twitter.com/freekmurze/staโ€ฆ
Ashraf Kamarudin liked on 14th April 2020
Robin Dirksen liked on 14th April 2020
Freek Van der Herten replied on 14th April 2020
Thereโ€™s a PR that will add it. So: soonโ„ข
Zayn Buksh replied on 14th April 2020
Hi @freekmurze, is it possible to do have logging levels like debug/error/info/etc using LD?
Zayn Buksh replied on 14th April 2020
Brilliant ๐Ÿ‘๐Ÿฝ
Freek Van der Herten replied on 14th April 2020
So, I created ld so I donโ€™t have to think which kind of arguments it can take. Ld can take anything.
Freek Van der Herten replied on 14th April 2020
What happens if you use a string as the second argument?
smknstd replied on 14th April 2020
ok
smknstd replied on 14th April 2020
I think it won't work, it has to be an array. There is a really short section on it in the laravel doc
smknstd replied on 14th April 2020
Isn't it really similar with PSR's context ? Laravel already lets you log it as a second argument php-fig.org/psr/psr-3/#13-โ€ฆ
Eduard Lupacescu retweeted on 14th April 2020
Eduard Lupacescu liked on 14th April 2020
Nuno Souto liked on 13th April 2020
Costas Loizou liked on 13th April 2020
Musa ๏ฃฟ liked on 13th April 2020
Tom liked on 13th April 2020
Reza liked on 13th April 2020
Anatoli Nicolae liked on 13th April 2020
Roman Pronskiy liked on 13th April 2020
Benjamin Guez liked on 13th April 2020
Kevin McKee liked on 13th April 2020
Willan Correia liked on 13th April 2020
Erick Patrick liked on 13th April 2020
Onyi baby... liked on 13th April 2020
Rob Gordijn liked on 13th April 2020
Swapnil Bhavsar ๐Ÿ’™ liked on 13th April 2020