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

  • 1
    Could you please share your observer file where you tried logging? – Agnes Oct 11 '19 at 6:15

Try Below Code:

Check LoggerInterface to debug

namespace Magento\Enquiry\Observer;
use \Magento\Framework\Event\ObserverInterface;
use \Magento\Framework\Event\Observer;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\ObjectManagerInterface;
use \Magento\Checkout\Model\Session as CheckoutSession;

class Index implements ObserverInterface
     protected $messageManager;
     protected $scopeConfig;
     protected $checkoutSession;
     protected $logger;
     protected $_coreSession;
     protected $_customerRepositoryInterface;

     public function __construct(
        ManagerInterface $messageManager,
        ScopeConfigInterface $scopeConfig,
        ObjectManagerInterface $objectmanager,
        CheckoutSession $checkoutSession,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\Session\SessionManagerInterface $coreSession,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
                    ) {
        $this->messageManager = $messageManager;
        $this->scopeConfig = $scopeConfig;
        $this->_objectManager = $objectmanager;
        $this->checkoutSession = $checkoutSession;
        $this->logger = $logger;
        $this->_coreSession = $coreSession;
        $this->_customerRepositoryInterface = $customerRepositoryInterface;

  public function execute(Observer $observer)

         $order = $observer->getEvent()->getOrder();
         $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        
         $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
         $store_id = $storeManager->getStore()->getStoreId();

         // shipping address
         $shippingAddress = $order->getShippingAddress(); 
         $city = $shippingAddress->getCity();
         $country = $shippingAddress->getCountryId();



Log print below path:-


I hope so it's use full.

| improve this answer | |
  • coreSession not been used anywhere is there any reason for that or is that just an accident you have added in the constructor? – jibin george Oct 11 '19 at 7:56
  • check I have updated my code @jibingeorge – Devidas Oct 11 '19 at 8:00
  • for some reason to use coreSession – Devidas Oct 11 '19 at 8:03

Try this one.

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/custom.log');
$logger = new \Zend\Log\Logger();
$logger->info('=== Your Comment Here ===');
$logger->info('Print Your Value: '.$yourvariable);
$logger->info('=== Your Comment Here ===');
| improve this answer | |
  • Thank you I will give this one a go too – Jack Brooks Oct 11 '19 at 6:33

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