The Below code is to log your data in var/log/system.log file. In the code, the data from observer is logged into var/log/system.log file . The following file is placed in app/code/Company/Module/Observer , under magento root.
You as per your requirement can log the data in any file, i,e. block, helper, controller, model, plugin files , etc.
<?php
namespace Company\Module\Observer;
use Psr\Log\LoggerInterface as Logger;
class CustomObserver implements ObserverInterface
{
/**
* @var Logger
*/
protected $_logger;
/**
* [__construct ]
*
* @param Logger $logger
*/
public function __construct(
Logger $logger
) {
$this->_logger = $logger;
}
/**@param \Magento\Framework\Event\Observer $observer
*
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$data = $observer->getEvent()->getData();
$this->_logger->info(print_r($data,true));
}
}
That’s it for this dev doc on How to Print Log data in Magento 2. Hope, it will help you. Please reach out to our team via a support ticket if you have any queries.
You may also check our wide range of Best Magento 2 Extensions including Elastic Search and many more.
Be the first to comment.