5

Just wondering if its possible to output an array to the Plugin::log call? Would be really handy to see if an error occurs, what a data object's values are!

Tried the following:

Plugin::log(array('test')), LogLevel::Error);
Plugin::log(json_decode(array('test')), LogLevel::Error);
Plugin::log(var_dump(array('test')), LogLevel::Error);
Plugin::log(print_r(array('test')), LogLevel::Error);
Plugin::log(\CVarDumper::dump(array('test')), LogLevel::Error);
6

Passing true as the second parameter to print_r returns a string that can be written to the log.

Plugin::log(print_r(array('test'), true));
3

Something like this should work for you:

Plugin::log(\CVarDumper::dumpAsString(array('test')), LogLevel::Error);

Note the \ in front of CVarDumper to tell PHP that class is not in the Craft namespace, but in the global one.

  • Looks good, but the following error Fatal error: Call to undefined method CVarDumper::export() – crawf Feb 27 '15 at 23:08
  • My bad... I guess I should actually read the error message. It's dump, not export. Updated the answer. – Brad Bell Feb 27 '15 at 23:10
  • Haha, yeah thats better :) Still outputs a blank row in the log 2015/02/27 23:12:37 [error] [plugin] – crawf Feb 27 '15 at 23:15
  • I think the example might be missing a parenthesis at the end of the first argument: \CVarDumper::dump(array('test')) – Ben Parizek Feb 27 '15 at 23:28
  • 2
    Didn't realize dump echo'd out the results. dumpAsString will work writing it to the log, though... just tested it. – Brad Bell Feb 28 '15 at 4:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.