How to Create a Custom Logger in Magento 2?

Magento 2 logger component is based on Monolog. Magento 2 has three types of logger handler class.

  1. System
  2. Debug
  3. Exception

Today I create my own logger, How to?

Step 1: Create module.xml


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="SR_CustomLogger" setup_version="2.0.0">
    </module>
</config>

Step 2: Create registration.php


\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'SR_CustomLogger',
    __DIR__
);

Step 3: create di.xml


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="SR\CustomLogger\Logger\Handler">
        <arguments>
            <argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
        </arguments>
    </type>
    <type name="SR\CustomLogger\Logger\Logger">
        <arguments>
            <argument name="name" xsi:type="string">customLogger
            <argument name="handlers"  xsi:type="array">
                <item name="system" xsi:type="object">SR\CustomLogger\Logger\Handler</item>
            </argument>
        </arguments>
    </type>
</config>


Step 4: Create Handler.php


namespace SR\CustomLogger\Logger;

class Handler extends \Magento\Framework\Logger\Handler\Base
{
    protected $fileName = '/var/log/custom_logger.log';
    protected $loggerType = \Monolog\Logger::INFO;
}

Step 5: Create Logger.php


namespace SR\CustomLogger\Logger;


class Logger extends \Monolog\Logger
{

}

Done!

So now test new logger. Here I am going to inject login event observer.

So create a events.xml [SR/CustomLogger/etc/frontend/events.xml]


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_customer_authenticated">
        <observer name="custom_logger_customer_authenticated" instance="SR\CustomLogger\Observer\Authenticated" />
    </event>
</config>

Create a Authenticated.php observer for that.



namespace SR\CustomLogger\Observer;

use Magento\Framework\Event\ObserverInterface;

class Authenticated implements ObserverInterface
{
    /**
     * @var \SR\CustomLogger\Logger\Logger $logger
     */
    protected $logger;

    /**
     * @param \SR\CustomLogger\Logger\Logger $logger
     */
    public function __construct(
        \SR\CustomLogger\Logger\Logger $logger
    ) {
        $this->logger = $logger;
    }

    /**
     * Upgrade customer password hash when customer has logged in
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        /** @var \Magento\Customer\Model\Customer $model */
        $model = $observer->getEvent()->getData('model');
        if($model->getId()) {
            $this->logger->info('TEST Custom Logger');
        }
    }
}

Download full module from Here

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s