How to Print Log in Magento 2

How to Print Log in Magento 2

Developers know the pain of errors and the process to deliver a working solution. Debugging can be made easier for them by custom logs. It helps to spot an error and the reason for it, easily. Logs contribute to the visibility into Magento 2 system processes.

Now if you want to learn how to print log in Magento 2, you’re at the right place! I have come up with 3 different methods to print log in Magento 2.

  1. Using Helper Data
  2. Using Logger Interface
  3. Using Object Manager

bin/magento config:set dev/debug/debug_logging 1

create file [Vendor]\[Module]\Helper\Data.php


<?php
namespace [Vendor]\[Module]\[Path];
use [Vendor]\[Module]\Helper\Data;
protected $helper;
class FileName
{
    public function __construct(Data $helperName)
    {
        $this->helper = $helperName;
    }
    $this->helper->printLog('msg for print');
}

Simply call Helper class method where you want to print the log :

create file [Vendor]\[Module]\[Path]\[fileName.php]