0

I am trying to log an array in an observer

If I do

Mage::log('prefix '.$orders,null);

I get the following in the system.log

2014-10-02T07:38:10+00:00 DEBUG (7): prefix Array

However if I try an log out the array with

Mage::log('prefix '. print_r($orders,true), null, 'logfile1.log', true);

The log file doesn't even get created. If I do

Mage::log($orders, null, 'logfile1.log', true);

Then a log file is created but it is completely empty. Can anyone advise in where I am going wrong. All I want to do is simply log out the contents of the array

3
  • try logging Mage::log(count($orders), null, 'logfile1.log', true); cause I bet your $orders collection is empty. Commented Oct 2, 2014 at 7:49
  • With your code the log file is created and inside it states 2014-10-02T07:52:36+00:00 DEBUG (7): 1 So I can presume from that the array is not empty
    – Goose84
    Commented Oct 2, 2014 at 7:52
  • You should be able to use var_export (php.net/manual/en/function.var-export.php) eg. Mage::log('prefix '. var_export($orders,true), null, 'logfile1.log', true); Commented Oct 2, 2014 at 9:07

2 Answers 2

2

I'm going to assume $orders is an array containing a multitude of Magento orders, which is probably why your print_r isn't working. Magento orders contain a lot of information of themselves.

To add, when dumped or printed, they also print out all the information of their items, which in turn print out all the information of their products, which in turn print out all the information of their attributes.. You see where I'm going with this?

I think you're running into a memory problem.

If you really do need to dump the contents of a Magento element, dump the specific part you want. i.e:

<?php print_r($order->getData()); ?>

That will pretty much always work, occasionally you'll into a NULL though, making var_dump a better choice than print_r.

4
  • Simply going to leave the suggestion to use Zend_Debug::dump($var) I certainly prefer it for convenience: from the description, This is a wrapper for var_dump() that adds the <pre /> tags, cleans up newlines and indents, and runs htmlentities() before output.
    – pspahn
    Commented Oct 2, 2014 at 8:12
  • @pspahn why would you want this in a log file? Commented Oct 2, 2014 at 8:55
  • @fschmengler I'm not sure why you're asking me that. I didn't mention a log file.
    – pspahn
    Commented Oct 2, 2014 at 9:11
  • @pspahn Not you, but OP Commented Oct 2, 2014 at 9:12
2

In addition to @Rian's answer: Magentos Varien_Object has a debug() method which returns all data in an array, calls getData() recursively and detects infinite loops.

So, what you should use is:

Mage::log('prefix '. print_r($orders->debug(), true), null, 'logfile1.log', true);

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