Magento logs print array contents

12,264

Solution 1

You have to return print_r as a string, so print_r($result, true) should do the trick.

Solution 2

Try it

$array = array('ID' => 1, 'NAME' => 'Amaresh', 'EMAIL' => '[email protected]');

Mage::log($array);

Output

2015-09-18T06:44:24+00:00 DEBUG (7): Array
(
    [ID] => 1
    [NAME] => Amaresh
    [EMAIL] => [email protected]
)
Share:
12,264
pokero
Author by

pokero

Updated on June 09, 2022

Comments

  • pokero
    pokero almost 2 years

    Mage::log works fine for me, except when it comes to printing array info.

    For example, if I have an array $result, and do this:

    Mage::log('[CartController: getDeliverLeadTime(country): ' . $result . ')');

    In my system.log file I just get: [CartController: getDeliverLeadTime~(country): Array)

    *I want it to print the actual structure and contents of the array, as a normal print_r would do.*

    If I do a var_dump of the array I get nothing, i.e. [CartController: getDeliverLeadTime~(country): ).

    If I do a print_r I get: [CartController: getDeliverLeadTime~(country): 1)

    This is a blocker for me as I can't debug what I need to - if anyone could shed any light on how to get Magento to actually print the contents of arrays in the logs, would be much appreciated.

    I read on alanstorm.com that its meant to be happen by default, but its not for me.

    Thanks Paul

  • pokero
    pokero almost 12 years
    Max thanks a million for that! Works perfectly, that is a big relief. I can mark the question as answered in 7 minutes, will do so then. Thanks again, Paul.
  • Anton S
    Anton S almost 12 years
    @pokero don't forget to vote up and accept the answer if it helped you out
  • Fiasco Labs
    Fiasco Labs over 11 years
    Nice to have print_r log stuff to your custom Magento log file.
  • Bosworth99
    Bosworth99 over 11 years
    huh. should be straightforward, but this messed me up a lot. easy fix.