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
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.
Add this code to any new PHP file in any folder or sun directories.
<?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 ?>
Create a file at the below path
app\code\Vendor\Extension\Block\Printlog.php
Now, add the below code.
<?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 [] [] } }
Add this code to any new PHP file in any folder or sun directories.
<?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 ?>
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!
In modern web development, seamless navigation and state management are crucial for delivering a smooth…
Magento Open Source 2.4.8 beta version released on October 8, 2024. The latest release of…
Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…
As the world of eCommerce continues to thrive, Shopify has become one of the most…
Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…
Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…