cancel
Showing results for 
Search instead for 
Did you mean: 

How to print log in magento 2

How to print log in magento 2

Hello Friends,

I am trying to print log in magento 2 but none of any log print is working in my installation.

I am trying like below.

namespace Magento\Demo\Block\Adminhtml\Product;
class Grid extends \Magento\Catalog\Block\Adminhtml\Product\Grid
{
	protected $_logger;
	protected $_eventManager;
	public function __construct(\Psr\Log\LoggerInterface $logger, \Magento\Framework\Event\ManagerInterface $eventManager,)
	{
		$this->_objectManager = $objectManager;
		$this->_logger = $logger;
		$this->_eventManager  = $eventManager;
		//parent::__construct($data);
	}
	
	protected function _prepareMassaction()
    {
		$this->_logger->addDebug("Prepare Mass Action Called");
		parent::_prepareMassaction();
		
	}
}

I have tried but nothing get printed in my system.log file as well as exception.log file.
I have tried so many blogs written by the developers but none of working.

Thanks for your answer in advance.

 

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.
6 REPLIES 6

Re: How to print log in magento 2

How to write a message to a log file? https://mage2.pro/t/15

https://mage2.pro/tags/log

Re: How to print log in magento 2

Hello @Dmitry_Fedyuk

Thanks for your answer.
I have tried your method of log printing but its not print anything in debug.log file.Smiley Sad

 

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: How to print log in magento 2

Maybe you have the logging disabled in the backend.

Re: How to print log in magento 2

Hello @Dmitry_Fedyuk

Can you please tell me how can i enable logging in magento 2 from admin or is any other way?

Thanks for your help in advance.

 

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: How to print log in magento 2

Oh, sorry, as I see, an ability to enable/disable logging by a backend option is missed in Magento 2: https://mage2.pro/t/1034

So the logging is always enabled in Magento 2.

Maybe something wrong in your code if it does not work.

Re: How to print log in magento 2

Hello Sanjayjethva,

Create a log file 'mylog.log' inside var/log/ and give the write permission.

Place this code inside your function.

  

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/mylog.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info("Here your cmment");