For some reason I couldn't use devel to debug output object arrays. Is there a way to output the array data to watchdog error reporting? This would also solve the issue of having to install or enable another module.
3 Answers
You can use watchdog but the message part should be translatable, so any variables should use placeholders defined in the variables array. Check out format_string to see how these should work.
watchdog(
'my_module_name',
'Here is the printed variable: <pre>@placeholder_name</pre>',
array('@placeholder_name' => print_r( $my_variable, TRUE)),
WATCHDOG_INFO
);
Also, convention dictates that the first parameter, $type
, should be the module name calling watchdog.
You can do the following to output array data to be viewed in watchdog:
// Directly use values.
watchdog("log_name", '<pre>' . print_r( $my_object, true) . '</pre>');
Then goto /admin/reports and look for "log_name".
-
1Watchdog runs through the translation function, so you need to use variables. See my answer. Jul 25, 2017 at 10:52
If you don't mind enabling another module, though, I wrote Object Log for just this kind of thing:
https://www.drupal.org/project/object_log
It's lightweight, easier to use in code, more user-friendly to view, and doesn't get pushed down the log as other log events are recorded.
-
rather not install another module. not exactly practical if I only needed to do it once to see the array output. thanks tho. Jan 26, 2016 at 0:06