How to create a custom log file in Magento 2?

You can use the below code for creating the custom log file

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/custom_log.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Your message');
If you are using Magento 2.4.3, please use the below code

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

Please leave your comment for asking any question or suggestion.

Need Help?

How to create a custom log file in Magento 2? expoundsofttechsolution why-choose-angular-in-2022-for-web-apps