1

In Magento 1.x I used to use

Mage::log();

I need the same function for Magento 2

Kyrylo Romantsov
  • 721
  • 2
  • 13
  • 35

3 Answers3

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

Some methods:

$this->_logger->addDebug($message); // log location: var/log/system.log
$this->_logger->addInfo($message); // log location: var/log/exception.log
$this->_logger->addNotice($message); // log location: var/log/exception.log
$this->_logger->addError($message); // log location: var/log/exception.log
$this->_logger->critical($e); // log location: var/log/exception.log    
Suresh Chikani
  • 15,836
  • 11
  • 62
  • 99
0

use following code in constructor of your class where you want to log

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

and use the following code to log

$this->logger->info($message);
$this->logger->debug($message);

magento noob
  • 655
  • 5
  • 17
0
First Create file mylog.log in path /var/log and then give writable permission after use below code
    $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/mylog.log');
    $logger = new \Zend\Log\Logger();
    $logger->addWriter($writer);
    $logger->info("Here your cmment");
akgola
  • 2,927
  • 1
  • 26
  • 57