Magento 2: Writing Custom Log

In Magento 1, you could simply write your own custom log with the following code:


Mage::log("Your Log Message", null, "your_log_file.log"); 

and it would be saved at MAGENTO_ROOT/var/log/your_log_file.log

However, it’s not that simple and quite different in Magento 2.

As Magento 2 uses dependency injection design pattern, you need to pass the logger instance through the constructor of your custom class. Magento 2 uses Monolog Library for logging purpose. See more at monolog’s Logger class at MAGENTO_ROOT/vendor/monolog/monolog/src/Monolog/Logger.php

In the below code, I am writing custom logs in one of the function of my Block class named ‘YourModule’ (app/code/Chapagain/YourModule/Block/YourModule.php).

As you can see from below code that you can write different types of logs like info, alert, notice, critical, error, debug, etc. The debug log is stored in debug.log file whereas all other logs are stored in system.log file.


<?php
namespace YourNamespace\YourModule\Block;
class YourModule extends \Magento\Framework\View\Element\Template
{
        protected $_logger;

	public function __construct(
		\Magento\Backend\Block\Template\Context $context,
		\Psr\Log\LoggerInterface $logger,
		array $data = []
	)
	{		
		$this->_logger = $logger;
		parent::__construct($context, $data);
	}
	
	public function testLogging() 
	{	
		// monolog's Logger class
		// MAGENTO_ROOT/vendor/monolog/monolog/src/Monolog/Logger.php
		
		$this->_logger->debug('debug1234'); 
		// saved in var/log/debug.log
		// [2016-02-04 04:48:44] main.DEBUG: debug1234 {"is_exception":false} []
		
		$this->_logger->info('info1234'); 
		// saved in var/log/system.log
		// [2016-02-04 04:52:56] main.INFO: info1234 [] []
		
		$this->_logger->alert('alert1234'); 
		// saved in var/log/system.log
		// [2016-02-04 04:52:56] main.ALERT: alert1234 [] []
		
		$this->_logger->notice('notice1234'); 
		// saved in var/log/system.log
		// [2016-02-04 04:52:56] main.NOTICE: notice1234 [] []
		
		$this->_logger->error('error1234'); 
		// saved in var/log/system.log
		// [2016-02-04 04:52:56] main.ERROR: error1234 [] []
		
		$this->_logger->critical('critical1234'); 
		// saved in var/log/system.log
		// [2016-02-04 04:52:56] main.CRITICAL: critical1234 [] []
		
		// Adds a log record at an arbitrary level
		$level = 'DEBUG';
		$this->_logger->log($level,'debuglog1234', array('msg'=>'123', 'new' => '456')); 
		// saved in var/log/debug.log
		// [2016-02-04 04:52:56] main.DEBUG: debuglog1234 {"msg":"123","new":"456","is_exception":false} []
		
		$level = 'ERROR';
		$this->_logger->log($level,'errorlog1234', array( array('test1'=>'123', 'test2' => '456'), array('a'=>'b') )); 
		// saved in var/log/system.log
		// [2016-02-04 04:52:56] main.ERROR: errorlog1234 [{"test1":"123","test2":"456"},{"a":"b"}] []		
		
	}
	
}
?>

The other way is using Object Manager directly. However, this is not encouraged in Magento 2. (http://magento.stackexchange.com/a/75204/1883)


\Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->debug('your debug message', array('test'=>'123'));
// saved in var/log/debug.log
// [2016-02-04 05:26:15] main.DEBUG: your debug message {"test":"123","is_exception":false} []

Hope this helps.
Thanks.