1

In Magento 1.x I used to use

Mage::log();

I need the same function for Magento 2

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    
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);

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");

Not the answer you're looking for? Browse other questions tagged or ask your own question.