preloder

How to create Custom Logger in Magento 2

Prev Next

Magento 2 Framework provides Logger component. This component is used across Magento 2 modules and framework as a mechanism to log errors and system information into files. You are aware of well-known system.log and debug.log files which are located in the Magento_Root/var/log directory.

The Logger component is based on Monolog PHP Library created by Jordi Boggiano. If you are looking for using Monolog for your project you may download this library via github, here is a link: https://github.com/Seldaek/monolog

Logger Handlers

The Magento\Framework\Logger component contains 3 handler classes. You may find these handlers in the Handler directory.

  • Setup
  • Exception
  • Debug

Typical Logger Handler defines it’s own log file and type of log information. Here is Debug class implementation.

namespace Magento\Framework\Logger\Handler;

use Monolog\Logger;

class Debug extends Base
{
    protected $fileName = '/var/log/debug.log';
    protected $loggerType = Logger::DEBUG;
}

Base Handler

All Handlers extend the Magento\Framework\Logger\Handler\Base class. The Base class is responsible for creating directories specified as part of the $fileName variable. Directories are created in the root directory of a Magento 2 project.

The Magento\Framework\Logger\Handler\Base class is configured in the main 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="system" xsi:type="object">Magento\Framework\Logger\Handler\System</item>
                <item name="debug" xsi:type="object">Magento\Framework\Logger\Handler\Debug</item>
            </argument>
        </arguments>
    </type>

As you may notice there is no Magento\Framework\Logger\Handler\Exception handler added as part of handlers. The Exception handler is managed by System handler. The reason behind it is to have single point of system-related logging handled by the System handler.

Custom Logger

In order to add custom logger we have to create new Handler class and extend it from the Magento\Framework\Logger\Handler\Base class as shown below:

namespace Pronko\Logger\Handler;

use Magento\Framework\Logger\Handler\Base;
use Monolog\Logger;

class Custom extends Base
{
    protected $fileName = '/var/log/pronko/custom.log';
    protected $loggerType = Logger::DEBUG;
}

In the di.xml configuration file we have to configure virtual type based on Monolog class.

  <virtualType name="customLogger" type="Magento\Framework\Logger\Monolog">
        <arguments>
            <argument name="handlers"  xsi:type="array">
                <item name="debug" xsi:type="object">Pronko\Logger\Handler\Custom</item>
            </argument>
        </arguments>
    </virtualType>

And inject it as a dependency of a class where you want to use custom log file.

 <type name="Pronko\CustomExtension\Model\Product">
        <arguments>
            <argument name="logger" xsi:type="object">customLogger</argument>
        </arguments>
    </type>

Alternatively you may want to declare different logger type. You may use customLogger virtual type.

 <virtualType name="customLogger" type="Magento\Framework\Logger\Monolog">
        <arguments>
            <argument name="handlers"  xsi:type="array">
                <item name="info" xsi:type="object">Pronko\Logger\Handler\Custom</item>
            </argument>
        </arguments>
    </virtualType>

Every time the $this->logger->info() method is used all information will be stored in our custom.log file located in the Magento_Root/var/log/pronko/ directory.

Max with his team of Magento enthusiasts won the Best Magento 1 to Magento 2 Migration Award at the Magento Imagine 2017. As CEO at Pronko Consulting, he is actively working with Magento 2, delivering superior customer experience. As one of the most famous developers on a market and in the Magento Community, he launched Magento 2 blog, Magento DevChannel – YouTube channel in 2017 where he shares his knowledge, experience and the best practices in the Magento field.

Posted on Mar 28, 2016

The Devletter

Subscribe to my Magento 2 tips and tutorials content.
Every week I send an interesting email directly to your inbox

Related Posts

Show
Hide
Pre-order the Magento 2 payment integration course TODAY PRE-ORDER
Hello. Add your message here.