Writing Quick Debug Logs in Magento 2

Writing Quick Debug Logs in Magento 2

When you need to see what is happening inside an observer or plugin, a temporary log file is often faster than a debugger.

Quick logging

$_product = $this->item->getProduct();
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info(print_r($_product->getData(), true));

This writes to var/log/test.log. Note that newer Magento versions use Laminas instead of Zend, and for permanent logging you should inject Psr\Log\LoggerInterface with a custom handler. Remove temporary logging before deploying.

Need a second pair of eyes on this? Our team handles Magento development every week. See how we can help.

Keep reading

Related articles

All articles

Need a hand?

Stuck on something similar?
We can fix it with you.

Send us the problem. A senior developer reads every message and replies within one working day.