---
title: "How to create custom Log file using Module in Magento 2"
url: "https://magecomp.com/blog/how-to-create-custom-log-file-using-module-in-magento-2/"
date: "2020-02-07T17:53:39+00:00"
modified: "2025-02-21T12:36:10+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 591
reading_time: "3 min read"
summary: "Hello, Magento Folks,"
description: "System logs provide you insights on critical errors, event tracking, and exceptions.find out how you can create the custom log file using the module in Magen..."
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Override Layout XML Block in Magento 2"
    url: "https://magecomp.com/blog/override-layout-xml-block-magento-2/"
  - title: "How to Remove Applied Cart Rules in Magento 2"
    url: "https://magecomp.com/blog/remove-applied-cart-rules-magento-2/"
  - title: "How to Create Admin Grid in Magento 2?"
    url: "https://magecomp.com/blog/create-admin-grid-magento-2/"
---

# How to create custom Log file using Module in Magento 2

_Published: February 7, 2020_  
_Author: Dhiren Vasoya_  

![How to create custom Log file using Module in Magento 2](https://magecomp.com/blog/wp-content/uploads/2020/02/How-to-create-custom-Log-file-using-Module-in-Magento-2-1024x512.webp)

Hello, Magento Folks,

Last time we learned how you can **[load products from ID and SKU](https://magecomp.com/blog/load-products-by-id-and-sku-in-magento-2/)**. Today, we are going to talk about how you can create a custom log file using a module in Magento 2.

[![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-2-2.webp)](https://magecomp.com/services/magento-upgrade-services/)

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="VendorExtensionLoggerHandler">
        <arguments>
            <argument name="filesystem" xsi:type="object">MagentoFrameworkFilesystemDriverFile</argument>
        </arguments>
    </type>
    <type name="VendorExtensionLoggerLogger">
        <arguments>
            <argument name="name" xsi:type="string">Extension</argument>
            <argument name="handlers" xsi:type="array">
                <item name="system" xsi:type="object">VendorExtensionLoggerHandler</item>
            </argument>
        </arguments>
    </type>
</config>
```

[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-2.webp)](https://magecomp.com/magento-performance-optimization.html)

**3.create Third file at given below path:**

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

```
<?php
namespace VendorExtensionLogger;

use MonologLogger;

class Handler extends MagentoFrameworkLoggerHandlerBase
{
    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 VendorExtensionLogger;

class Logger extends MonologLogger
{
}
```

**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="VendorExtensionCronCronrun" 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 VendorExtensionCron;

class Cronrun
{
    protected $_logger;

    public function __construct(
        VendorExtensionLoggerLogger $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](https://magecomp.com/support/)**.

[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-2.webp)](https://magecomp.com/services/hyva-theme-development/)

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

Happy Coding!


---

_View the original post at: [https://magecomp.com/blog/how-to-create-custom-log-file-using-module-in-magento-2/](https://magecomp.com/blog/how-to-create-custom-log-file-using-module-in-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-10-02 10:41:12 UTC_  
