13

What are the different way a developer can log messages in Magento 2.4+? Zend logger can't be used anymore either.

1

7 Answers 7

51

This will work with the newest Magento 2.4.3+:

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

This is just an example, but you have to adjust it this way.

1
  • worked for me too, easy and best way Feb 28, 2022 at 5:36
12

There are many ways to log messages currently and they are described below. Please do add on to this as a knowledge base.

  • Log to the existing Magento log files in var/log/

This is done using Psr\Log\LoggerInterface and is described in detail at https://devdocs.magento.com/guides/v2.4/config-guide/log/log-magento.html. This is the quickest, and also follows Magento best practices.

  • Log to a custom log file (not so quick)

This is done by extending Magento\Framework\Logger\Handler\Base and is described in detail at https://devdocs.magento.com/guides/v2.4/config-guide/log/custom-logger-handler.html. This can be followed in scenarios where you have a custom log file for a specific category of logs, maybe logs for a particular API integration. This is relatively longer to implement, and follows Magento best practices.

  • Log to any log file (quick throwaway of sorts)

This is done using Laminas\Log\Logger and can be used for quick debugging using logs without involving the constructor DI and running setup:di:compile every time. This is not a good way, as it bypasses the constructor DI for instantiating classes.

$writer = new \Laminas\Log\Writer\Stream(BP . '/var/log/custom.log');
$logger = new \Laminas\Log\Logger();
$logger->addWriter($writer);

// $logger has log methods for different priorities like warning, notice, error, etc.
$logger->info('Hello World!');
$logger->notice('Hello World!');
$logger->warn('Hello World!');
  • Of course, xDebug. Please use xDebug for debugging purposes without writing debug log statements all over the codebase.

Update (Sep 22, 2021): To use the Laminas_Log module in Magento 2.4.3, please run composer require laminas/laminas-log as Magento 2.4.3 removes this module. Credit to @Derek Lee.

8

After the release of the Magento 2.4.3 version, the Laminas-log module is removed. Thus, the previous method by using

    $writer = new \Laminas\Log\Writer\Stream(BP . $path);
    $logger = new \Laminas\Log\Logger();
    $logger->addWriter($writer);

is not work now.

To solve this problem, there is a temp fix by install the Laminas Log module again by using the composer:

Add the following code in composer.json's require

    "require": {
    ...
    "laminas/laminas-log": "*",
    ...
    },

This will make the Magento 2.4.3 support for the laminas Log.

1
2

You can use several formats.

Default logger

1: In Magento default format

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

public function logExample() {

    //To print string Output in debug.log
    $this->_logger->addDebug('Your Text Or Variables'); 
    
    // To print array Output in system.log
    $this->_logger->log('600', print_r($yourArray, true));

}

Custom logger

2: This will work with the older version of Magento 2.4.3:

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Your text message');
$logger->info(print_r($array, true)); //to log the array

3: This will work with the newest Magento 2.4.3+. it is tested on Magento 2.4.4, 2.4.6 version.

$writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom.log');
$logger = new \Zend_Log();
$logger->addWriter($writer);
$logger->info('Your text message');
$logger->info(print_r($array, true)); //to log the array
2
  • For 2.4.3+ incase of array how should I print it?
    – Avesh Naik
    Oct 7, 2023 at 10:29
  • @AveshNaik, Thank you. I have added the array printing option in the answer Oct 12, 2023 at 13:34
1

Quickest way to debug:

\Magento\Framework\App\ObjectManager::getInstance()
   ->get('Psr\Log\LoggerInterface')->debug('Your Message');

Some other ways:

From M2.3.5, use this Laminas log library's snippet:

$writer = new \Laminas\Log\Writer\Stream(BP . '/var/log/test.log');
$logger = new \Laminas\Log\Logger();
$logger->addWriter($writer);
$logger->info('Your text message');

Before M2.3.5, use this Zend log library's snippet:

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Your text message');

Tips for PHPStorm: Create a snippet shortcut to make this faster by adding new Live Templates on PHPStorm:

Go to Files -> Prefences -> Editor -> Live Templates -> Add new:

  • Abbreviation: log
  • Template text: the above log snippet
  • Place $END$ to your desired cursor position

Then type log and press the tab key, IDE will auto append the above snippet

1
  • Informative summary of answers
    – Joel Davey
    Mar 14, 2022 at 15:56
0
try {
        $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom.log');
    } catch (\Zend_Log_Exception $e) {
        // throw exception
    }
    $logger = new \Zend_Log();
    $logger->addWriter($writer);
    $logger->info('Event Called!');
-3

You can simply put this code where you are debugging

   $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/custom.log');
   $logger = new \Zend\Log\Logger();
   $logger->addWriter($writer);
   $logger->info('Custom text message');

Here in info() put your query or variable or whatever and hit the file. After that you will see the custom.log file in your Magento root folder var -> log -> custom.log

2
  • Thanks, @Haider141. But I believe Zend_Log is no more available in Magento 2.4 and above. Hence my post to use Laminas_Log in the third point.
    – Adarsh M
    Jun 23, 2021 at 13:17
  • You're right, @Haider141. Zend_Log does in fact work, but is not maintained anymore framework.zend.com and Magento has a local copy of the deprecated Zend framework with magento/zendframework1 for compatibility. This is why it works, but no Zend file should be used as a best practice, in my opinion. I have no assumed anything either. Deprecated/abandoned code shouldn't be used unless there is no alternative, hence my post.
    – Adarsh M
    Jul 1, 2021 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.