In this blog, I will instruct you a very useful speedy debug/hack for Magento 2 developers. Here we will look how you can speedy log in a custom log file in Magento 2.
I assumed you know about use log by Psr\Log\LoggerInterface. But, with the help of LoggerInterface you can log in either system.log or debug.log file based on the log level. In Magento2, You can also log to a custom file using Monolog. But, you need to perform too much of work if you just want to log for debugging purpose.
In Magento1, Logging to a custom file was very simple. you can log by just one line of code in a custom file as mentioned below.
Mage::log('MageClues.com', null, 'custom_file.log', true);
So now look, how I can achieve similar result in Magento 2.
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/custom_file.log'); $logger = new \Zend\Log\Logger(); $logger->addWriter($writer); $logger->info('MageClues.com');
This code will create a log file named custom_file.log under var/log/ folder and log ‘MageClues.com’ in it.
I hope this article is very helpful for you. Please share with your colleagues/developers.