How to Create a Custom Log in Magento 2?

When you are developing a website, you may find the need to log the variables or the custom messages created for the website. While Magento 2 has a built-in log facility based on Monolog library, there will come a time when you need to create a custom log. You will find the default monolog along the path “MAGENTO2_ROOT/vendor/monolog”

The main log facility class defined for Magento 2 is “Magento\Framework\Logger\Monolog” which is defined along the path “MAGENTO2_ROOT/app/etc/di.xml” using the following code

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

The monolog/logger class has been extended to this class

/** * Copyright © 2015 Magento. All rights reserved. * See COPYING.txt for license details. */
namespace Magento\Framework\Logger;
use Monolog\Logger;
class Monolog extends Logger

Using two arguments, message string and optional array parameter, you can create logs for the class defined.

Here are some examples of how you can use the two methods defined

$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

When creating logging using Magento 1, a static method was used


In Magento 2, an instance “Magento\Framework\Logger\Monolog” alongm with a critical method will be adopted for this purpose, as shown in the code below


How will you get the instance of Magento/Framework/Logger/Monolog in the class defined by you

namespace Test\Test\Model;
class Example{// instance of $e will be converted to string (magic metod __toString() will be called).

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’); }

You can easily achieve this using a constructor of the class defined by you, as seen in the code above.

Now you can use the instance “$this->_logger” in the class “Test\Test\Model\Example”

Let’s say you want to log in a custom file located in a custom location. You will need to create a custom log handler. There are three handlers, which you can use, namely exception, system, and debug defined along the path “MAGENTO2_ROOT/app/etc/di.xml” file

<type name=”Magento\Framework\Logger\Monolog”> <arguments> <argument name=”name” xsi:type=”string”>main</argument> <argument name=”handlers” xsi:type=”array”> <item name=”exception” xsi:type=”object”>Magento\Framework\Logger\Handler\Critical</item> <item name=”system” xsi:type=”object”>Magento\Framework\Logger\Handler\System</item> <item name=”debug” xsi:type=”object”>Magento\Framework\Logger\Handler\Debug</item> </argument> </arguments>

With this code, you know how to create the custom log to log in your custom file.

In case the log object exists, you don’t need to send the log through the constructor.

This is just a basic into creating custom log when working on Magento 2 platform. You will see that you need to define the custom file as well as custom location to log your data when working on Magento 2. Check if you need a constructor or not when working with Magento 2 to create custom log files

Note: A backup of your existing files is essential

Deepa, a technical writer with Semaphore Software, who now devotes her time in advising its clients to hire offshore magento developers. Her love for reading helps her constantly provide latest information on different technical and design aspects of Magento