cakephp log an array as var_dump
Asked Answered
C

3

11

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.

Cicero answered 14/9, 2012 at 13:19 Comment(1)
I simply did var_dum($myarray); in my controller and I got the resulted array printed in my view. may it help.Bendicta
S
19

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);
Splashdown answered 14/9, 2012 at 13:26 Comment(0)
B
2

Try:

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

The true parameter makes print_r return the value rather than print on screen, so you can save it.

https://www.php.net/manual/en/function.print-r.php

Boehmenist answered 14/9, 2012 at 13:22 Comment(0)
C
0

Somebody got a redirection method presented here.

This I have used to see what I have there, and it shows very clear.

Cicero answered 14/9, 2012 at 13:32 Comment(1)
This guy uses ob_start() to get the return value of a var_dump(), which is what print_r($var, true) does internally. Seems he is using an old version of php.Boehmenist

© 2022 - 2024 — McMap. All rights reserved.