How To

How to create custom Log file using Module in Magento 2

Hello, Magento Folks,

Last time we learned how you can load products from ID and SKU. Today, we are going to talk about how you can create a custom log file using a module in Magento 2.

Logging is an essential part that provides insights for various system-related processes in Magento 2. With log data, you can easily track errors, important events, exceptions, and more. When we are creating a website or a module on Magento 2, it is very important that we also create a log file to monitor/debug the errors.

Default Magento 2 comes with built logging components based on the monolog library. But today, we are going to see how you can create a custom log file using module programmatically in Magento 2. So, without further ado, let’s get started.

First, create a custom log file at the below path:

1.create file at given below path:

app\code\Vendor\Extension\etc\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="Vendor_Extension" setup_version="1.0.0" schema_version="1.0.0" />
</config>

2.create second file at given below path:

app\code\Vendor\Extension\etc\di.xml

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

3.create Third file at given below path:

app\code\Vendor\Extension\Logger\Handler.php

<?php
namespace Vendor\Extension\Logger;

use Monolog\Logger;

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

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

4.create fourth file at given below path:

app\code\Vendor\Extension\Logger\Logger.php

<?php
namespace Vendor\Extension\Logger;

class Logger extends \Monolog\Logger
{
}

5.create fifth file at given below path:

app\code\Vendor\Extension\etc\crontab.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="default">
    <job name="custom_cronjob" instance="Vendor\Extension\Cron\Cronrun" method="execute">
        <schedule>0 */12 * * *</schedule>
    </job>
</group>
</config>

6.create sixth file at given below path:

app\code\Vendor\Extension\Cron\Cronrun.php

<?php
namespace Vendor\Extension\Cron;

class Cronrun
{
    protected $_logger;

    public function __construct(
        \Vendor\Extension\Logger\Logger $logger
    ) {
        $this->_logger = $logger;
    }

    public function execute()
    {
        $this->_logger->info(“ Hello This is our Custom log file example”);
    }
}

After running this module, Your file will be generated on below path -Magento2_root_directory\var\log\vendor_extension_log_file.log

So, this was it for the day. You can implement and customize these codes right away as per your needs. If you face any problem while implementation, then you can get in touch with us at our support desk.

Lastly, please share this with your Magento partners and let us know what they feel about this.

Happy Coding!

Click to rate this post!
[Total: 6 Average: 3.7]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

3 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

5 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

7 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago