1

I am trying to put log in observer

sales_order_save_after Whenever I put logger in the execute I can’t do any checkout it complains about guest checkout payment Is there any other way I could see the log

When I do var_dump it takes me to cart page from checkout because I am stopping the payment

2

2 Answers 2

6

Try Below Code:

Check LoggerInterface to debug

<?php
namespace Custom\Module\Observer;

use \Magento\Framework\Event\ObserverInterface;
use \Magento\Framework\Event\Observer;

class Index implements ObserverInterface
{
     protected $logger;

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

         $order = $observer->getEvent()->getOrder();
         // shipping address
         $shippingAddress = $order->getShippingAddress(); 
         $city = $shippingAddress->getCity();
         $country = $shippingAddress->getCountryId();
         $this->logger->info("shippingAddress=>".$country."<");
         $this->logger->info("shippingAddress=>".$city."<");

        }
  }

Log print below path:-

../var/log/system.log

I hope so it's use full.

3
  • coreSession not been used anywhere is there any reason for that or is that just an accident you have added in the constructor? Commented Oct 11, 2019 at 7:56
  • check I have updated my code @jibingeorge
    – Devidas
    Commented Oct 11, 2019 at 8:00
  • for some reason to use coreSession
    – Devidas
    Commented Oct 11, 2019 at 8:03
0

Try this one.

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/custom.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('=== Your Comment Here ===');
$logger->info('Print Your Value: '.$yourvariable);
$logger->info('=== Your Comment Here ===');
1
  • Thank you I will give this one a go too Commented Oct 11, 2019 at 6:33

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