logo

Can we log data with complex placeholders?

Or only simple scalar types?

Joel Clermont
Joel Clermont
2025-10-30

In a previous tip, I explained why you should use placeholders in logs.

Someone recently asked me how these placeholders would work with data types like arrays.

For example, what if you wanted to do this? Would it work?

$params = ['key' => 'foo', 'keys' => ['aaa', 'bbb']];

Log::info("Error {key} not in {keys}", $params);

The quick answer is yes, it works! You'd get output like this:

Error foo not in array["aaa","bbb"] {"key":"foo","keys":["aaa","bbb"]} 

The $params value has to be an array, but each of the keys within that array can have values of other data types. As long as PHP can cast that data type to a string, it will display.

PHP's built-in array to string conversion has us covered here, but if we had a more complex data type, or wanted more control over the conversion, we could manually cast the value and not rely on the logger to do it automatically.

Here to help,

Joel

P.S. I bet you have a Laravel question rolling around in your head right now that would be perfect for our Mastering Laravel community. Come ask it!

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.