14

I am trying to print array variable contents into a log file.

In Magento 1, it was possible using Mage::log(print_r($arr, 1), null, 'logfile.log');

For Magento 2, in class file I have written following code:

protected $_logger;

    public function __construct(\Psr\Log\LoggerInterface $logger) {
        $this->_logger = $logger;
    }


private function getValuesAsHtmlList(\Magento\Framework\Object $object) {
        $options = $this->getOptions($object);
       //$this->_logger->addDebug($options );
        $this->_logger->log(100,null,$options);
    }

When I execute the code after clearing the cache, Debug.log & system.log files are not showing the array contents.

Please share if anyone has any idea about it.

0

6 Answers 6

19

Suppose your array is

$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));

then you have to write below code to write proper array format in your log file

$this->_logger->log(100,print_r($a,true));

It will print in you log file

[2015-11-09 06:58:27] main.DEBUG: Array
(
    [a] => apple
    [b] => banana
    [c] => Array
        (
            [0] => x
            [1] => y
            [2] => z
        )

)
 {"is_exception":false} []
1
  • With this approach, I needed to look at the var/log/debug.log file for the output
    – Mike Dubs
    May 3, 2023 at 21:12
11

See declaration of log method

public function  \Psr\Log\LoggerInterface::log($level, $message, array $context = array());

So, you need code like

$this->_logger->log(100, json_encode($options));
2
  • I'd print_r($options, true) myself instead of json encoding. But preferences \o/ Nov 8, 2015 at 16:05
  • 5
    better yet: $this->_logger->debug(json_encode($options));
    – nevvermind
    Nov 9, 2015 at 22:50
6

This method works well for me.

$this->logger->info(print_r($myArray, true));

Then check your system.log file.

1

For array and also object just use

public function __construct(\Psr\Log\LoggerInterface $logger) {
        $this->_logger = $logger;
    }

public function logs(){

$this->logger->info(print_r($orderData, true));
}

and check the output in /var/log/debug.log file

0
0
protected $_logger;

    public function __construct(\Psr\Log\LoggerInterface $logger) {
        $this->_logger = $logger;
    }

public function logs(){
  $level='log';
$this->_logger->log($level,'errorlog1234', array( array('test1'=>'123', 'test2' => '456'), array('a'=>'b') ));

}

Try This it will print array. Tested !

0

I see core file uses var_export:

//File: vendor/magento/module-paypal/Model/AbstractIpn.php
/**
 * Log debug data to file
 *
 * @return void
 */
protected function _debug()
{
    if ($this->_config && $this->_config->getValue('debug')) {
        $this->logger->debug(var_export($this->_debugData, true));
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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