In Magento 1.x I used to use
Mage::log();
I need the same function for Magento 2
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityprotected $_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
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);
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");