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

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

6 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago