Pretty print an array/object in watchdog

Public

This allows to print complex array or object (example : $node ) in a more human-readable way in the watchdog.

</> CopyGet raw version
php
  1. watchdog("my_module", '<pre>' . print_r( $my_array_or_object, true) . '</pre>');

Comments

Randy Fay's picture

Actually, you never want to do it this way, at least on a multilingual site, because you'll end up with 50 gazillion different watchdog strings to translate. The second parameter of a watchdog string is supposed to be translatable.

doubouil's picture

This may explain why my locale tables sometimes end up weighting more than 5M ! Thanks for the heads up, I guess i now have to make a query to delete everything containing a <pre>

drupalcodefactory's picture

I guess better solution for debug is to use dpm($object) from devel module. Which is nice way rather then using while debug

rwohleb's picture

To avoid the translation issue, why don't you just take advantage of the variable support?

  1. watchdog('my_module', '<pre>@print_r</pre>', array('@print_r', print_r( $my_array_or_object, TRUE)));
mamadou's picture

Useful !

deepesh reshi's picture

  1. watchdog('error message', var_export('$variable_to_show', TRUE));
  2. the $variable_to_show here could be a string, an array, or an object.
Benjamin Couderc's picture

  1. rwohleb commented 1 year 10 months ago
  2.  
  3. To avoid the translation issue, why don't you just take advantage of the variable support?
  4.  
  5. watchdog('my_module', '<pre>@print_r</pre>', array('@print_r', print_r( $my_array_or_object, TRUE)));

You got one thing wrong mate.
 array('@print_r', print_r( $my_array_or_object, TRUE))

should be

 array('@print_r' => print_r( $my_array_or_object, TRUE))