Tag Archive tricks

Magento 2 – New logging function compatible with version 2.4.3

Magento 2 debug log 

In a previous article, I explained how I add temporary debug logs with magento 2 in my code while coding. It can be very useful, it saves me time and does not require installing additional tools. You just have to remember that this solution is used temporarily and must not be committed in the code.

The complete post is here : https://www.addeos.com/magento-2-adding…n-when-debugging

Please the read the post again to understand how to add this as a live template in phpStorm which is the IDE I am using all the time.

With Magento version 2.4.3, I now use the following function which is still working.

The logging function has to be updated to :

<?php
private function log($str)
{
    $str = 'CLASS : ' . str_pad(__CLASS__, 50, ' ')
        . ' - LINE : ' . debug_backtrace()[0]['line']
        . ' - FUNCTION : ' . str_pad(debug_backtrace()[1]['function'], 15, ' ')
        . ' - STR : ' . $str;
    $filesystem = new \Magento\Framework\Filesystem\Driver\File();
    $handler = new \Magento\Framework\Logger\Handler\Base($filesystem, 'var/log/', 'custom_logs.log');
    $loggerHandlers = ['debug' => $handler];
    $logger = new \Monolog\Logger('customlogger', $loggerHandlers);
    $logger->addDebug($str);
}