Search code examples
phpmodx

how to echo/print a nicely formatted array to the modx log?


I'm trying to figure out how to print/echo a formatted array to the modx error log. but print_r & pre tags do not work, if I use something like:

$log = "<pre>";
$log .= print_r($formdata);
$log .= "</pre>";
$this->modx->log(modX::LOG_LEVEL_ERROR, 'Form Data = ' . $log);

the result in the log is:

[2014-12-20 22:35:18] (ERROR @ /index.php) Form Data = <pre>1</pre>

I have seen formatted arrays in the modx logs before, does anyone know how to do it?


Solution

  • in print_r() function add 2nd argument TRUE for return output value, see below sample code

    $log = "<pre>";
    $log .= print_r($formdata, true);
    $log .= "</pre>";
    $this->modx->log(modX::LOG_LEVEL_ERROR, 'Form Data = ' . $log);