magento2_custom_log_file magento2_custom_log_file

How to create custom log file in module in Magento 2

Magento 2 provides three types of log files debug.log, exception.log, and system.log. Based on your need you can log the data in any of these log files. Below is a code snippet of how to write a log in Magento 2. If you want to log data into your custom log file then you need to create a Logger class first.

Use Default Log File in Custom Module in Magento 2

<?php

namespace CodextblogTestBlock;
class Test extends MagentoFrameworkViewElementTemplate
{
    protected $_logger;

    public function __construct(
        MagentoBackendBlockTemplateContext $context,
        PsrLogLoggerInterface $logger,
        array $data = []
    )
    {        
        $this->_logger = $logger;
        parent::__construct($context, $data);
    }

    public function LogData()
    {
        // saved data in var/log/debug.log
        $this->_logger->debug('message goes here');
        // saved data in var/log/system.log
        $this->_logger->info('message goes here');  
    }
}

When your store goes live these log files are getting big in size day by day. If you have stored your custom module log in any of these log files then it will become very much difficult to check the log when something goes wrong.

The ideal way of logging custom module data is into custom log file. Today in this post we will see how to create log file in custom module in Magento 2.

To create a custom log file follow the below steps and add the below-mentioned files in your custom module.

Step 1: Define custom logger handler in module’s di.xml file.

<?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="CodextblogTestLoggerHandler">
        <arguments>
            <argument name="filesystem" xsi:type="object">MagentoFrameworkFilesystemDriverFile</argument>
        </arguments>
    </type>
    <type name="CodextblogTestLoggerLogger">
        <arguments>
            <argument name="name" xsi:type="string">customLogger</argument>
            <argument name="handlers"  xsi:type="array">
                <item name="system" xsi:type="object">CodextblogTestLoggerHandler</item>
            </argument>
        </arguments>
    </type>
</config>

Step 2: Create Logger.php class inside Logger directory.

<?php


namespace CodextblogTestLogger;

class Logger extends MonologLogger
{
}

Step 3: Create Handler.php class inside Logger directory.

<?php


namespace CodextblogTestLogger;

use MagentoFrameworkLoggerHandlerBase;
use MonologLogger;

class Handler extends Base
{
    protected $loggerType = Logger::INFO;

    protected $fileName = '/var/log/custom.log';
}

In this file, you can define the custom log file name. Here we have defined our custom log file name custom.log

Use Custom Log File in Custom Module in Magento 2

Now we have already created our custom logger class. We can use this class inside any of the PHP class and log the data into our custom log file.

<?php

namespace CodextblogTestBlock;
class Test extends MagentoFrameworkViewElementTemplate
{
    protected $_customLogger;

    public function __construct(
        MagentoBackendBlockTemplateContext $context,
        CodextblogTestLoggerLogger $customLogger,
        array $data = []
    )
    {        
        $this->_customLogger = $customLogger;
        parent::__construct($context, $data);
    }

    public function LogData()
    {
        //save data in var/log/custom.log
        $this->_customLogger->info('Message goes here');

       //add array in log
       $this->_customLogger->info('API Params', $params); 
    }
}
Written by

Chirag

Chirag is a Founder & eCommerce Technology Leader at MageArmy. He leads the eCommerce development team and drives the company’s technology vision, helping businesses build, scale, and optimize high-performing online stores. With years of experience in eCommerce development, he has worked with businesses across industries to solve complex technical challenges and create scalable digital commerce solutions. He loves sharing practical insights about eCommerce development, Magento, Adobe Commerce, and the latest trends in digital commerce to help online businesses grow and succeed.