1

This question already has an answer here:

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

marked as duplicate by fmsthird, Shoaib Munir, Mohit Kumar Arora, Sumit, Dhiren Vasoya Oct 11 at 10:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

Try Below Code:

Check LoggerInterface to debug

<?php
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();
         $this->logger->info("storeid==>".$store_id."<");


         // 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.

  • 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 at 7:56
  • check I have updated my code @jibingeorge – Devidas Oct 11 at 8:00
  • for some reason to use coreSession – Devidas Oct 11 at 8:03
0

Try this one.

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/your_custom_log.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 ===');
  • Thank you I will give this one a go too – Jack Brooks Oct 11 at 6:33

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