Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

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.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month 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

What are your thoughts on "A package to dump anything to the log"?

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.