Magento Tutorials

How To Print Log In Magento 2

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.

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.

<?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.

<?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.

<?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!

Click to rate this post!
[Total: 7 Average: 4.4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

16 hours ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

3 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

3 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

4 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

6 days ago

HYVĂ„ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We have brought exciting news for Magento store owners. Hyvä Themes recently released 1.3.6 and…

6 days ago