What are the different way a developer can log messages in Magento 2.4+? Zend logger can't be used anymore either.
7 Answers
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.
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.
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.
-
to me dosn't make sense to add a new laminas-log dependency I think it is a better solution to create the log class pixiemedia.co.uk/blog/magento-zend-support-ending.html Sep 29, 2021 at 11:19
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
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!');
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');
3: This will work with the newest Magento 2.4.3+. it is tested on Magento 2.4.4 version.
$writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom.log');
$logger = new \Zend_Log();
$logger->addWriter($writer);
$logger->info('Your text message');
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
-
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 MJun 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 MJul 1, 2021 at 6:15