How to print logs in Magento 2?
How to print logs in Magento 2?

How to print logs in Magento 2?

Logs in Magento 2 consist of system information records for the analysis in the future. One of the most common examples of such events is the error log.

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.

Main Magento 2 log class is Magento\Framework\Logger\Monolog and is defined in MAGENTO2_ROOT/app/etc/di.xml

<preference for="Psr\Log\LoggerInterface" type="Magento\Framework\Logger\Monolog"/>

Here in this article, I will show you 3 methods to print logs in Magento 2.

Method 1: To print temporary logs with a new file

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/logfile.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Simple Text Log');
$logger->info('Array Log'.print_r($myArrayVar, true));

In the first line, you need to define a new file name that is created in /var/log directory with new file name(logfile.log) at the time execution of this code. You can create any folder with any name or subfolder to print the log in the new file.

To print simple text use $logger->info('Simple Text Log');

To print array use: $logger->info('Array Log'.print_r($myArrayVar, true));

Method 2: By using \Psr\Log\LoggerInterface class

protected $_logger;

public function __construct (
    ...
    \Psr\Log\LoggerInterface $logger
    ...
) {
    $this->_logger = $logger;
}

public function logExample()
{
    $this->_logger->debug('Testing Debug'); 

    $this->_logger->info('Testing Info');         
    
    $this->_logger->alert('Testing Alert'); 
    
    $this->_logger->notice('Testing Notice'); 
    
    $this->_logger->error('Testing Error'); 
    
    $this->_logger->critical('Testing Critical Error'); 
    
    $level = 'DEBUG';
    $this->_logger->log($level,'Test Debug', array('msg'=>'123', 'new' => '456')); 
    
    $level = 'ERROR';
    $this->_logger->log($level,'Testing Log', array( array('test1'=>'test1', 'test2' => 'test2'), array('a'=>'b') )); 

    $this->_logger->log('600', print_r($yourArray, true));
}

Here is the explanation and output for each methods:

//Print log in var/log/system.log
$this->_logger->info('Testing Info');
//Output: [2019-02-22 10:52:56] main.INFO: Testing Info [] []
//Print log in var/log/debug.log
$this->_logger->debug('Testing Debug');
//Output: [2019-02-22 10:48:44] main.DEBUG: Testing Debug {"is_exception":false} []
// Write to default log file: var/log/system.log<br>
$this->_logger->notice('Testing Notice');
//Output: [2019-02-22 10:52:56] main.NOTICE: Testing Notice [] []
// Write to default log file: var/log/system.log
$this->_logger->alert('Testing Alert');
//Output: [2019-02-22 10:52:56] main.ALERT: Testing Alert [] []
// Write to default log file: var/log/system.log
$this->_logger->error('Testing Error');
//Output: [2019-02-22 10:52:56] main.ERROR: Testing Error [] []
// Write to default log file: var/log/system.log
$this->_logger->critical('Testing Critical Error');
//Output: [2019-02-22 10:52:56] main.CRITICAL: Testing Critical Error [] []
// Adds a log record at an arbitrary level
$level = 'DEBUG';
//Print log in var/log/debug.log
$this->_logger->log($level,'Test Debug', array('msg'=>'123', 'new' => '456'));
//Output: [2019-02-22 10:52:56] main.DEBUG: Test Debug {"msg":"123","new":"456","is_exception":false} []
//To print log in: var/log/system.log<br> $level = 'ERROR';
$this->_logger->log($level,'Testing Log', array( array('test1'=>'test1', 'test2' => 'test2'), array('a'=>'b') )); //Output: [2019-02-22 10:52:56] main.ERROR: Testing Log [{"test1":"test1","test2":"test2"},{"a":"b"}] []
//To print array Output in system.log
$this->_logger->log('600', print_r($yourArray, true));

Method 3: Using ObjectManager

To print string Output in debug.log

\Magento\Framework\App\ObjectManager::getInstance()
   ->get('Psr\Log\LoggerInterface')->debug('Your Message');

To print array Output in system.log

$myArray = array('test1'=>'123', 'test2'=>'123', 'test3'=>'123');
$level = '100'; // use one of: 100, 200, 250, 300, 400, 500, 550, 600
\Magento\Framework\App\ObjectManager::getInstance()
    ->get('Psr\Log\LoggerInterface')
    ->log($level, print_r($myArray, true));