Today we are going to talk about Magento 2 logging. Logging is the most effective and important way for developers to find out where things are going wrong in their code. Magento 2 comes with built-in logging feature which is based on Monolog library. You can find this library under “/vendor/monolog” of your Magento 2 installation. Magento 2 logging can also be achieved by using Zend logger library, we are going to cover several approaches below

There are several ways you can achieve creating Magento 2 logging either in system.log or exception.log under /var/ of your Magento 2 installation. But today we are going to cover two simple way of doing it, one is with dependency injection and one is without dependency injection.

Let’s start with simple one first using Zend logger library

 $writer = new ZendLogWriterStream(BP . '/var/log/custom.log'); $logger = new ZendLogLogger(); $logger->addWriter($writer); $logger->info("My first log is created under /var/log/custom.log"); 

The above will allow you to add your custom log file in var/log. It comes with the following severity options -:

 $logger->info("My first info log is created under /var/log/custom.log"); $logger->debug("My first debug log is created under /var/log/custom.log"); $logger->emerg("My first emerg log is created under /var/log/custom.log"); $logger->alert("My first alert log is created under /var/log/custom.log"); $logger->crit("My first crit log is created under /var/log/custom.log"); $logger->err("My first err log is created under /var/log/custom.log"); $logger->warn("My first warn log is created under /var/log/custom.log"); $logger->notice("My first notice log is created under /var/log/custom.log"); 

Second option is to use Monolog library

 <?php
namespace ScommerceCustomModel; class Custom{ protected $_logger; public function __construct( PsrLogLoggerInterface $logger, array $data = [] ) { $this->_logger = $logger; parent::__construct($data); } public function customMethod() { /* your custom method code */ $this->_logger->addDebug('My first debug log is created under /var/log/system.log'); }
} 

It also comes with the following severity options which you will be able to call using the following code -:

 $this->_logger->addInfo("My first info log is created under /var/log/system.log"); $this->_logger->addDebug("My first debug log is created under /var/log/system.log"); $this->_logger->addNotice("My first notice log is created under /var/log/system.log"); $this->_logger->addWarning("My first warning log is created under /var/log/system.log"); $this->_logger->addAlert("My first alert log is created under /var/log/system.log"); $this->_logger->addError("My first error log is created under /var/log/exception.log"); $this->_logger->addCritical("My first critical log is created under /var/log/exception.log"); $this->_logger->addEmergency("My first emergency log is created under /var/log/exception.log"); 

The above will allow you to add the logging in any of your custom module using dependency injection and if you are using some of the Magento classes like MagentoFrameworkViewElementTemplate then you don’t need to add dependency in your constructor as it is already part of the template class.

Third option is to create your own handler

Step 1 – Create di.xml file in your custom module under ScommerceCustometc

<type name="ScommerceCustomLoggerHandler"> <arguments> <argument name="filesystem" xsi:type="object">MagentoFrameworkFilesystemDriverFile</argument> </arguments>
</type>
<type name="ScommerceCustomLoggerLogger"> <arguments> <argument name="name" xsi:type="string">Scommerce/Custom</argument> <argument name="handlers" xsi:type="array"> <item name="debug" xsi:type="object">ScommerceCustomLoggerHandler</item> </argument> </arguments>
</type>

Step 2 – Create Handler.php file in your custom module under ScommerceCustomLogger

namespace ScommerceCustomLogger; /** * Class Handler * @package ScommerceCustomLogger */
class Handler extends MagentoFrameworkLoggerHandlerBase
{ /** * @var string */ protected $fileName = '/var/log/custom.log';
}

Step 3 – Create Logger.php file in your custom module under ScommerceCustomLogger

namespace ScommerceCustomLogger; /** * Class Logger * @package ScommerceCustomLogger */
class Logger extends MagentoFrameworkLoggerMonolog
{ }

Step 4 – Use the logger in your custom class

namespace ScommerceCustomModel; /** * Class Handler * @package ScommerceCustomLogger */
class CustomClass { /**
* @var ScommerceCustomLoggerLogger
*/
protected $_logger; /**
* @param ScommmerceCustomLoggerLogger $logger
*/
public function __construct( ScommmerceCustomLoggerLogger $logger
{ $this->_logger = $logger;
} }

It also comes with the following severity options which you will be able to call using the following code -:

 $this->_logger->addInfo("My first info log is created under /var/log/custom.log"); $this->_logger->addDebug("My first debug log is created under /var/log/custom.log"); $this->_logger->addNotice("My first notice log is created under /var/log/custom.log"); $this->_logger->addWarning("My first warning log is created under /var/log/custom.log"); $this->_logger->addAlert("My first alert log is created under /var/log/custom.log"); $this->_logger->addError("My first error log is created under /var/log/custom.log"); $this->_logger->addCritical("My first critical log is created under /var/log/custom.log"); $this->_logger->addEmergency("My first emergency log is created under /var/log/custom.log"); 

Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.

Similar Posts