cakephp log an array as var_dump

user529543 picture user529543 · Sep 14, 2012 · Viewed 10.1k times · Source

I need to jump into a server side code. It is used cakephp there. I would like to see a variable, I think it is a model, but I am not sure, let be a variable in or case.

CakeLog::write('debug', 'myArray'.var_export($myArray) );

it will have the output

myArray: Array

I would like to see similar output as var_dump can produce to the output.

Is that possible? if yes, than how?

Any help apreciated.

Answer

Niklas Modess picture Niklas Modess · Sep 14, 2012

Just use print_r, it accepts a second argument not to output the result.

CakeLog::write('debug', 'myArray'.print_r($myArray, true) );

And if you don't want new lines, tabs or double spaces in your log files:

$log = print_r($myArray, true);
$log = str_replace(array("\n","\t"), " ", $log);
$log = preg_replace('/\s+/', ' ',$log);
CakeLog::write('debug', 'myArray' . $log);