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="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="SR_CustomLogger" setup_version="2.0.0">

Step 2: Create registration.php


Step 3: create di.xml

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

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



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="" 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" />

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.