How to Create a Custom Log in Magento 2 ?

Super additional features and architecture has set Magento 2 edition apart in the market. In most of the development procedures, it is required to custom message or log variable.

In this article, I will illustrate how to create a custom log in Magento 2. On basis of the Monolog library, Magento 2 does have a built-in log facility. This is available at
[code]
‘MAGENTO2_ROOT/vendor/monolog’.

[/code]

The actual Magento 2 log facility class is
[code]‘Magento\Framework\Logger\Monolog’.
[/code]
You will get the refined definition in

[code]‘MAGENTO2_ROOT/app/etc/di.xml’

[/code]
check the table below:

<preferencefor=”Psr\Log\LoggerInterface”type=”Magento\Framework\Logger\Monolog” />

Check the extension class below from the monolog:

[code]

/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\Logger;

use Monolog\Logger;

class Monolog extends Logger
{
}
[/code]

In the class ‘Monolog/Logger’ you will find a number of fascinating methods to create logs. The basic two methods for the argument are: the message (string) and the optional array parameter (where you can pass instance of object).

Check the method below:

[code]
$this->_logger->;addDebug($message); // log location: var/log/system.log
$this->_logger->addInfo($message); // log location: var/log/exception.log
$this->_logger->addNotice($message); // log location: var/log/exception.log
$this->_logger->addError($message); // log location: var/log/exception.log
$this->_logger->critical($e); // log location: var/log/exception.log
[/code]

Check out the example for logging php exception:

The static method is used in Magento 1:

[code]Mage::logException($e);[/code]

Whereas, in Magento 2 an instance of “Magento\Framework\Logger\Monolog” is used.  Along with the ‘critical’ method for logging exception from try-catch.

[code]$this->_logger->critical($e);[/code]
// instance of $e will be converted to string (magic metod __toString() will be called).

Starting with an example of getting instance of ‘Magento\Framework\Logger\Monolog’ in the class.

All the instances pass via class constructor. This is because Magento 2 uses dependency injection.

If you need to use the object ‘Magento\Framework\Logger\Monolog’ then the instance should be passed via constructor of the class.

Check the code in the below given table:

[code]
namespace Insync\Test\Model; class Example{ protected $_logger; public function __construct( \Psr\Log\LoggerInterface $logger, //log injection array $data = [] ) { $this->;_logger = $logger;
parent::__construct($data);
}
public function someExampleMethod() {
/*
some logic of method
*/
//accessing to logger instance and calling log method
$this->;_logger->;addDebug(‘some text or variable’);
}
}
[/code]

Well, can you see that we passed ‘\Psr\Log\LoggerInterface $logger’ in class via constructor? This is done to utilize the log object in this class.

Now, you can use instance
[code]‘$this->_logger ‘in class’ Insync\Test\Model\Example’.[/code]

You need to create a custom log handler, if you need to write a log in the custom file name that also in a custom file location. There are 3 kinds of handlers i.e. debug, exception and system defined in the Magento 2 log class same as ‘MAGENTO2_ROOT/app/etc/di.xml’ file. Check the table:

[code]
main

Magento\Framework\Logger\Handler\Critical
Magento\Framework\Logger\Handler\System
Magento\Framework\Logger\Handler\Debug

[/code]

For instances like every html block class that extends ‘Magento\Framework\View\Element\Template’ or in the model class that extends ‘\Magento\Framework\Model\AbstractModel’. In few classes the log object already exists, so there is no need to pass log object via constructor.

The ‘parent’ class already has property ‘$_logger’ instance of: Magento\Framework\Logger.

Finally you are done!!! I really hope overview of Magento 2 logging was useful.

Integrate your ERP with eCommerce stores, Marketplace and CRM