How to create custom log file in module in Magento 2

magento2_custom_log_file

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 Codextblog\Test\Block;
class Test extends \Magento\Framework\View\Element\Template
{
    protected $_logger;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Psr\Log\LoggerInterface $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="Codextblog\Test\Logger\Handler">
        <arguments>
            <argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
        </arguments>
    </type>
    <type name="Codextblog\Test\Logger\Logger">
        <arguments>
            <argument name="name" xsi:type="string">customLogger</argument>
            <argument name="handlers"  xsi:type="array">
                <item name="system" xsi:type="object">Codextblog\Test\Logger\Handler</item>
            </argument>
        </arguments>
    </type>
</config>

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

<?php


namespace Codextblog\Test\Logger;

class Logger extends \Monolog\Logger
{
}

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

<?php


namespace Codextblog\Test\Logger;

use Magento\Framework\Logger\Handler\Base;
use Monolog\Logger;

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 Codextblog\Test\Block;
class Test extends \Magento\Framework\View\Element\Template
{
    protected $_customLogger;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Codextblog\Test\Logger\Logger $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'); 
    }
}

Want to ask a question or leave a comment?

Leave a Comment

(0 Comments)

All the comments are goes into moderation before approval. Please do not spam. Your email address will not be published. Required fields are marked *

Enjoy this post? Please support Us!

Follow us on twitter for daily new updates..

 

Want to become
a Magento 2 Expert?

If Yes! Then Subscribe to our newsletter and get weekly article to you email id.
Subscribe Here
SUBSCRIBE NOW
close-link

To Avoid Spam Downloads, We Want Your Email

We will send you download link right
away. Please submit form below.
SEND ME DOWNLOAD LINK
Close