Sevalla is the all-in-one PaaS for your web projects. Host and deploy your applications, databases, object storage, and static sites. Enjoy advanced deployment pipelines, a complete database studio, instant preview apps, and one-click templates. The pricing is simple: no hidden fees, no seat-based pricing, and you pay only for what you use. Get real human support from developers.

Get started now with a $50 credit at Sevalla.com.

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.