Quantcast
Channel: Webkul Blog
Viewing all articles
Browse latest Browse all 5555

log data in magento2

$
0
0

log data in magento2

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('Data', $data);
    }
}

Viewing all articles
Browse latest Browse all 5555