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


The actual Magento 2 log facility class is
You will get the refined definition in


check the table below:

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

Check the extension class below from the monolog:


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

namespace Magento\Framework\Logger;

use Monolog\Logger;

class Monolog extends Logger

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:

$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

Check out the example for logging php exception:

The static method is used in Magento 1:


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

// 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:

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

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:




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