Skip to main content
🎁 Exclusive Launch Deal: Premium Themes Available FREE — Grab Yours Now!
Back to Blog
General

Magento2 custom log

admin 2 min readPublished Feb 1, 2023Updated Jul 1, 2026
Magento2 custom log

In today's blog we will explain about Magento2 Custom Logs, but before that we will understand why we need logs and why we need custom logs.

What is Log?

Logs provides the visibiltiy that what is going in code process , It helps you to debug your code while running application. also you can store some helpful information like error if and when this error triggered.

Magento2 Provides 2 types of log, file based log and database log

Magento 2 Supports MonoLog which provides wide range of logging handlers

Magento has below inbuilt log functions

  • alert()
  • critical()
  • debug()
  • emergency()
  • error()
  • info()
  • log()
  • notice()
  • warning()

Below is the example , how you can write a log for your module from magento's default logging.

You need to inject \Psr\Log\LoggerInterface in your class

Below is example

logger = $logger;
    }

    public function aroundGet(ProductRepositoryInterface $subject,callable $proceed,$sku)
    {
        $this->logger->info(" Some Logging");
    }

}

Write a Custom Log File

First of all you need to create a custom logger class file which will extend Magento\Framework\Logger\Handler\Base class

Now Write a below code in di.xml file


        
            
                Mageacademy\LogExample\Logger\CustomLogger
            
        
    
    
        
            MyCustomLogger
        
    

Here you can see we have used virtual type and argument replacement feature, If you are not aware about it you can visit our Virtual Type in magento 2 and Argument Replacement in Magento2 Blog

Now you can below code to log your file

logger = $logger;
    }

    public function aroundGet(ProductRepositoryInterface $subject,callable $proceed,$sku)
    {
        $this->logger->info(" Some Logging");
    }

}

Here you can use info , cirtical as per your requirement.

Hope you find my this blog , Please share with your friends and visit us again.