2
 /** @var \Magento\Quote\Model\Quote $quote */
    $quote = $this->session->getQuote();
    /** @var \Magento\Quote\Model\Quote\Item[] $quoteItems */
    $quoteItems = $quote->getAllItems();
    foreach ($quoteItems as $quoteItem) {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/express.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info(100,print_r($quoteItems,true));
        if (in_array($quoteItem->getSku(), $this->disabledForSku)) {
            return false; // disable the method if we found product with specified sku
        }
    }

This is the code I am using for print_r but this is displaying blank page.

  • 1
    yes because it will write your log into var/log/express.log file . Go to that directory and check that file . – Manthan Dave Jun 30 '17 at 5:16
  • that file is showing empty 0B – Raghu Jun 30 '17 at 5:16
  • No for m2 its not , check this link for more details - inchoo.net/magento-2/magento-2-logging – Manthan Dave Jun 30 '17 at 5:18
  • thanks. but actually what i need is, i want the values of foreach in a new log – Raghu Jun 30 '17 at 5:20
5

print_r with Magento models often results in exhausting the memory limit because it prints all the dependencies which can be hundreds of complex objects, sometimes even in circular dependencies.

Instead, use print_r($object->debug()), the debug method only returns the _data array instead of all properties and also detects recursion.

Since you are dealing with an array, you have to call debug on all items. You can let Magento do it automatically for you by putting the array into a simple DataObject:

print_r((new \Magento\Framework\DataObject(['items' => $quoteItems]))->debug());

or map the array yourself:

print_r(array_map(function($item) { return $item->debug(); }, $quoteItems));
0

You can use like below code.

$writer_fail = new \Zend\Log\Writer\Stream(BP . '/var/log/express.log');
$logger_fail = new \Zend\Log\Logger();
$logger_fail->addWriter($writer_fail);
$fail = "you can set your error here";
$logger_fail->info($fail);

please make ensure the log file is proper permission is to be set.

  • where can i put print_r ? – Raghu Jun 30 '17 at 5:21
0

This Works for me if you want to print arrays in log

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/express.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info(print_r($quoteItems, true));

If you are using windows make sure you change slash for path you define

\var\log\express.log
  • $quote = $this->session->getQuote(); /** @var \Magento\Quote\Model\Quote\Item[] $quoteItems */ $quoteItems = $quote->getAllItems(); foreach ($quoteItems as $quoteItem) { if (in_array($quoteItem->getSku(), $this->disabledForSku)) { return false; // disable the method if we found product with specified sku } } – Raghu Jun 30 '17 at 6:39
  • after foreach i have kept your log but still showing blank page, i also did flus h cache – Raghu Jun 30 '17 at 6:39
  • after foreach try this $logger->info(print_r($quoteItem->getData(), true)); – Priyank Jun 30 '17 at 6:45
  • did the same but no result – Raghu Jun 30 '17 at 6:51

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.