Hire Me
How to write Log in Magento 2

How to Write Log in Magento 2

Debugging can be a nightmare if developers Magento didn’t had the feature of logging. Logs help to spot an error and the reason for it. In this blog we will learn about it and the process to use it. Let’s get started.

Introduction

Magento uses Monolog library by default. Monolog is a popular PHP logging solution with a wide range of handlers that enable you to build advanced logging strategies. It is implemented as a preference for Psr\Log\LoggerInterface in Magento application using di.xml.

Main Magento 2 log class is Magento\Framework\Logger\Monolog and is defined in MAGENTO2_ROOT/app/etc/di.xml

<preference for="Psr\Log\LoggerInterface" type="Magento\Framework\Logger\Monolog"/>

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.

Usage

We have two methods to write log in Magento 2:

1. Using \Psr\Log\LoggerInterface class

To start working with a logger, you must create an instance of \Psr\Log\LoggerInterface. With this interface, you can call the following functions to write data to log files:

Example:

class SomeClass
 {
     private $logger;

     public function __construct(\Psr\Log\LoggerInterface $logger)
     {
         $this->logger = $logger;
     }

     public function doSomething()
     {
         try {
             //do something
         } catch (\Exception $e) {
             $this->logger->debug('msg to print'); // printed in var\log\debug.log
    	     $this->logger->info('msg to print');  // printed in var\log\system.log
         }
     }
 }

You may log JSON, Arrays etc too just like we did with string in above example.

2. Using \Zend\Log\Writer\Stream class (Logging into Custom File)

In the first method, we can write log into system.log and debug.log, but there are multiple logs being written into same files from Magento core files etc. In some cases, we might need to write log into custom files. It’s very simple, here is a sample code:

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Your text message');

The above code might not work with the Magento version > 2.3.x. You may use this instead:

$writer = new \Zend_Log_Writer_Stream(BP . '/var/log/test.log');
$logger = new \Zend_Log();
$logger->addWriter($writer);
$logger->info("Your text message");

In this case, the message would be logged into var/log/test.log

I hope this blog would help you in the development process and debugging. If you need any more help, feel free to leave a comment or contact me.

About Author

4 Replies to “How to Write Log in Magento 2”

  1. Hello are using WordPress for your site platform? I’m new to the blog world but I’m trying to get
    started and create my own. Do you require any html coding expertise to make your
    own blog? Any help would be really appreciated!

Leave a Reply

Your email address will not be published. Required fields are marked *