error_log("test\n" . json_encode($json_string, JSON_PRETTY_PRINT));
json_encode()
will actually not necessarily produce JSON: it will produce something that can be read by javascript. If you give it an array or an object, it will produce JSON; if you give it a string, it will produce a javascript string. And that’s what you’re doing, so that’s what you’re getting.
To be clear, $json_string
is a string: (as far as PHP is concerned, it’s a string; if you passed that same string to javascript, it would be interpreted as an object). You pass that through json_encode()
and all you’re going to get is another string (a string of doubly-encoded JSON).
JSON_PRETTY_PRINT
is having no effect here, because you’re not producing JSON: you’re producing something that javascript too would see as a string.
Savvy?
So what you need to do is to (a) turn $json_string
back into a PHP array, and then (b) reencode that as JSON, this time using the JSON_PRETTY_PRINT
flag.
$log_array = json_decode($json_string, true);
$json_pretty_string = json_encode($log_array, JSON_PRETTY_PRINT);
error_log('test' . PHP_EOL . $json_pretty_string);
Rather than converting it back to a PHP array and then back to JSON, it would be better to add the JSON_PRETTY_PRINT
flag to wherever you’re getting $json_string
from in the first place, if possible.
Alternatively, just log $json_string
directly (no need to encode it: it’s already a string, you can pass it to error_log()
as it is), and worry about prettifying it only when you need to read your logs. This will make your logs considerably smaller.