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 Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

1 day ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

4 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

4 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

4 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

5 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago