Back to Top

How To Print Log Data In Magento 2

Updated 28 March 2024

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.

Searching for an experienced
Magento 2 Company ?
Find out More
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home