How to create Custom Logger in Magento 2

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.

[php]
namespace Magento\Framework\Logger\Handler;

use Monolog\Logger;

class Debug extends Base
{
protected $fileName = ‘/var/log/debug.log’;
protected $loggerType = Logger::DEBUG;
}
[/php]

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.

[php]
<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>
[/php]

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:

[php]
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;
}
[/php]

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

[php]
<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>
[/php]

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

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

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

[php]
<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>
[/php]

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.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *