Back to Top

Quick Logging in Custom File in Magento 2

Updated 16 January 2026

We need quick logging in Magento 2 to debug code instantly without setting up full logging infrastructure.

It allows developers to track errors, variables, or execution flow while developing or troubleshooting modules.

Quick logs are useful when you want immediate insights into code behavior, especially during module development, API calls, or cron jobs.

Magento 2 provides three main ways to create logs, and developers commonly use all of them depending on the scenario.

You can check the overview in the video below —

Searching for an experienced
Magento 2 Company ?
Find out More

1. Log Using Psr\Log\LoggerInterface

What Is LoggerInterface?

Magento 2’s core logger implements PSR‑3, making it easy to log messages like info or error.
You will see logged messages appear in system.log or debug.log depending on the log level.

How to Log Using LoggerInterface

Inject the LoggerInterface into your class constructor for dependency injection.

    public function __construct(
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->logger = $logger;
    }

Then call $this->logger->info('text') in any method to write to default Magento log files.

2. Log to a Custom File (Monolog Custom Module)

What Is Custom Log File Using Monolog?

Magento 2 lets you create a custom Monolog channel and handler to log messages to your own file.
This gives you a separate log like var/log/customfile.log instead of system/debug log.

Steps to Create It

Step 1. Add your handler and logger definition in etc/di.xml to register the custom log channel.

<?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="Webkul\CustomModule\Logger\Logger">
    <arguments>
      <argument name="name" xsi:type="string">customfile</argument>
      <argument name="handlers" xsi:type="array">
        <item name="system" xsi:type="object">Webkul\CustomModule\Logger\Handler</item>
      </argument>
    </arguments>
  </type>
</config>

step 2. Create Handler.php

<?php

namespace Webkul\CustomModule\Logger;

class Handler extends \Magento\Framework\Logger\Handler\Base
{
    /**
     * @var string
     */
    protected $fileName = '/var/log/customfile.log';
}

step 3. Create Logger.php

<?php

namespace Webkul\CustomModule\Logger;

class Logger extends \Monolog\Logger
{
}

3. Log Directly With Zend_Log

What Is Direct Logging With Zend_Log?

For quick debugging within code, you can use Zend_Log with a writer to stream to a custom file.
This approach writes directly without setting up a full custom logger class or DI configuration.

Example Code Snippet

$writer = new \Zend_Log_Writer_Stream(BP .'/var/log/customfile.log');
$logger = new \Zend_Log();
$logger->addWriter($writer);
$logger->info('hello log');

Conclusion

Magento 2 offers multiple ways to create custom logs, making it easier to isolate and analyze module-level issues.

Quick inline logging with Zend_Log_Writer_Stream adds even more flexibility when you need immediate insights during development.

Looking to improve your store’s speed and overall performance? Check out our Magento 2 Speed & Optimization services.

For expert guidance or custom feature development, you may hire our Magento 2 developers to support your project.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home