Magento 2: Replacement for Mage::log method?
In magento2, You can also write to the logs using the Zend
library like below :
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Your text message');
Edited
You can also print PHP objects and arrays like below :
$logger->info(print_r($yourArray, true));
protected $logger;
public function __construct(\Psr\Log\LoggerInterface $logger)
{
$this->logger = $logger;
}
You use debug, exception, system for PSR Logger for example:
$this->logger->info($message);
$this->logger->debug($message);
HINT:
Don't forget to run php bin/magento setup:di:compile
\Magento\Framework\App\ObjectManager::getInstance()
->get(\Psr\Log\LoggerInterface::class)->debug('message');