How to Print Log in Magento 2
To err is human; to solve, divine!
Logs in Magento 2 consist of system information records for the analysis in the future. One of the most common examples of such events is the error log.
Developers know the pain of errors and the process to deliver a working solution. Debugging can be made easier for them by custom logs. It helps to spot an error and the reason for it, easily. Logs contribute to the visibility into Magento 2 system processes.
Now if you want to learn how to print log in Magento 2, you’re at the right place! I have come up with 3 different methods to print log in Magento 2.
Follow any of the below methods to create your own custom log files and get to solve the errors 🙂
Note:
- Make sure that the debug mode is enabled in the backend. Go to Stores -> Configuration -> Advanced -> Developer -> Debug and set “Yes” to Log to File.
- Make sure your Magento store is in developer mode.
- For production mode, you have to run the below command in shell:
1 |
bin/magento config:set dev/debug/debug_logging 1 |
Methods to Print Log in Magento 2:
Method: 1Â Using Helper Datacreate file [Vendor]\[Module]\Helper\Data.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php namespace [Vendor]\[Module]\Helper; use Zend\Log\Writer\Stream; use Zend\Log\Logger; class Data extends AbstractHelper { public function printLog($log) { $writer = new Stream(BP . '/var/log/fileName.log'); $logger = new Logger(); $logger->addWriter($writer); $logger->info($log); } } |
Simply call Helper class method where you want to print the log :
create file [Vendor]\[Module]\[Path]\[fileName.php]
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php namespace [Vendor]\[Module]\[Path]; use [Vendor]\[Module]\Helper\Data; protected $helper; class FileName { public function __construct(Data $helperName) { $this->helper = $helperName; } $this->helper->printLog('msg for print'); } |
Method 2:Â Using Logger Interface
To enable the log in custom extension, go to app\code\[Vendor]\[Module]\[Path]\[fileName.php] and add below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php namespace [Vendor]\[Module]\[Path]; use Psr\Log\LoggerInterface; class FileName { protected $logger; public function __construct(LoggerInterface $logger;) { $this->logger = $logger; } $this->logger->debug('msg to print'); // print var\log\debug.log $this->logger->info('msg to print'); // print var\log\system.log } |
Method 3:Â Using ObjectManager
To print the message in var\log\system.log
1 |
\Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('msg to print'); |
Implement any of the above code to print log in Magento 2.
Carry on your development process smoothly and if anytime you need to log variables or custom messages, you’ll be having the easy method!
Please feel free to ask your doubts in the Comments section below. I’d be happy to solve them asap.
Rate the post with 5 stars if it helped you ease your code debugging ?
Happy Debugging!
Still need help? Hire our Adobe-certified Magento experts.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How To Customize 404 Page Not Found in Magento 2
How to Upgrade Magento to the Latest Version
Next