Hello Magento Friends,
Logs are records of system information that can be examined in the future. The error log is one of the considerable instances of such events.
Developers are familiar with the agony of mistakes and the steps involved in delivering a functioning solution. Custom logs can make debugging easier for them. It makes it simple to identify a mistake and the cause of the issue. The insight into Magento 2 system processes is aided by logs.
Sometimes, developers need to check some data in Magento 2 but don’t want the data printed on the website. To fulfill this objective, they write in log files. Let’s check the steps to print logs in Magento 2. A virtual private server is useful for conducting business analysis, as it provides a secure and customizable environment for data processing and analysis.
Contents
Steps To Print Log In Magento 2
Before we follow any of these methods make sure your Magento store is in the Developer mode. Check if your store is in production mode then execute this command.
php bin/magento config:set dev/debug/debug_logging 1
There are three ways to print your log in the Magento store.
Method 1: To print temporary logs with a new file
Add this code to any new PHP file in any folder or sun directories.
1 2 3 4 5 6 7 |
<?php $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/mylogfile.log'); $logger = new \Zend\Log\Logger(); $logger->addWriter($writer); $logger->info('This Is Simple Text Log'); //To print simple text log $logger->info(print_r($myarray, true)); //To print array log ?> |
Method 2: Using Logger Interface
Create a file at the below path
app\code\Vendor\Extension\Block\Printlog.php
Now, add the below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?php namespace Vendor\Extension\Block; use Psr\Log\LoggerInterface; class Printlog { protected $logger; public function __construct(LoggerInterface $logger;) { $this->logger = $logger; } public function logExample() { //Print log in var/log/system.log $this->logger->info('Testing Info'); //Output: [2022-02-01 13:33:42] main.INFO: Testing Info [] [] //Print log in var/log/debug.log $this->logger->debug('Testing Debug'); //Output: [2022-02-01 13:33:42] main.DEBUG: Testing Debug {"is_exception":false} [] // Write to default log file: var/log/system.log $this->logger->notice('Testing Notice'); //Output: [2022-02-01 13:33:42] main.NOTICE: Testing Notice [] [] // Write to default log file: var/log/system.log $this->logger->alert('Testing Alert'); //Output: [2022-02-01 13:33:42] main.ALERT: Testing Alert [] [] // Write to default log file: var/log/system.log $this->logger->error('Testing Error'); //Output: [2022-02-01 13:33:42] main.ERROR: Testing Error [] [] // Write to default log file: var/log/system.log $this->logger->critical('Testing Critical Error'); //Output: [2022-02-01 13:33:42] main.CRITICAL: Testing Critical Error [] [] } } |
Method 3: Using ObjectManager
Add this code to any new PHP file in any folder or sun directories.
1 2 3 4 5 |
<?php $objectmanager = \Magento\Framework\App\ObjectManager::getInstance(); $objectmanager->get('Psr\Log\LoggerInterface')->info('Testing Log'); //Print log in var/log/system.log $objectmanager->get('Psr\Log\LoggerInterface')->debug('Testing Log'); //Print log in var/log/debug.log ?> |
Conclusion
For writing the log, you can use any of the methods listed above. Also, you can check our article on How to create a custom log file in Magento 2. Last but not least, if you found the information useful, please leave your valuable feedback in the comment box below, and if you have any questions when applying the above article, share with us. We’ll write the finest solution post for you, and don’t forget to share it with your Magento friends so they may receive assistance with their problems as well.
Happy Developing!