Magento 2 custom Log

Magento 2 is using Zend framework 2 as its core library which comes with 8 different types of logs.

To print log you just need to use any of this below method in your block, helper or model class file.

$this->_logger->emergency(’emergency method called’);
$this->_logger->alert(‘alert method called’);
$this->_logger->critical(‘critical method called’);
$this->_logger->info(‘Info method called’);
$this->_logger->error(‘error method called’);
$this->_logger->warning(‘warning method called’);
$this->_logger->notice(‘notice method called’);
$this->_logger->debug(‘debug method called’);

  • Where Info, Error, Warning, Notice type are generated in system.log file.
  • Debug type generated in debug.log file.
  • Emergency, Alert and Critical type are generaetd in exception.log file.

Log settings like Magento 1.x from admin configuration is not available in Magento 2.

Here i will show you how to print log in your desired file name.

$writer = new \Zend\Log\Writer\Stream(BP . ‘/var/log/test.log’);
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info(‘Informational message’);

It will print your message in test.log file.

Leave a comment