6

for 2.4.2 below code working

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

for 2.4.2 above code working

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

Above solution not working in Magento 2.4.3 version anybody have an idea for the same

For 2.4.3?

2

4 Answers 4

8

the custom log file created using the below code

for Magento 2.4.2 before version use this

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

for Magento 2.4.2 or after version use this

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

for Magento 2.4.3 version use this

$writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom.log');
$logger = new \Zend_Log();
$logger->addWriter($writer);
$logger->info('text message');
1
  • Hold on, why is 2.4.3 still using Zend? Aug 16, 2021 at 18:08
3
$writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom.log');
$logger = new \Zend_Log();
$logger->addWriter($writer);
$logger->info('text message');
1

Here the correct way to create log file

magento/app/code/vendorname/modulename/etc/di.xml

<!-- start logger-->

    <virtualType name="Vendor\ModuleName\Log\Handler" type="Magento\Framework\Logger\Handler\Base">
        <arguments>
            <argument name="fileName" xsi:type="string">/var/log/custom_logger_name.log</argument>
        </arguments>
    </virtualType>

    <virtualType name="Vendor\ModuleName\Log\Logger" type="Monolog\Logger">
        <arguments>
            <argument name="name" xsi:type="string">Vendor::ModuleName</argument>
            <argument name="handlers"  xsi:type="array">
                <item name="system" xsi:type="object">Vendor\ModuleName\Log\Handler</item>
            </argument>
        </arguments>
    </virtualType>

    <type name="Vendor\ModuleName\Model\ClassWhereNeedToInjectCustomLogger">
        <arguments>
            <argument name="logger" xsi:type="object">Vendor\ModuleName\Log\Logger</argument>
        </arguments>
    </type>

<!-- end logger-->

On the constructor of this class Magento\app\code\Vendor\ModuleName\Model\ClassWhereNeedToInjectCustomLogger inject Psr\LoggerInteface on the constructor and use the methods, it will write on your new file log.

Cheers

0

Execute below command to include log library into the vendor/laminas if not there.

This has been removed from 2.4.3.

For Magento 2.4.3 version, execute this - composer require laminas/laminas-log -vvv

1
  • Thank you @sandip, but Magento has removed this module in version 2.4.3 so something reason behind it. so we need to find out another solution instead of adding again laminas log. Aug 16, 2021 at 11:20

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.