I want to create a custom log file in Magento 2.4.5 .

Before, I was using the following method:

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/Custom_Token.log');
$this->logger = new \Zend\Log\Logger();

Guess its not working in 2.4.5 version. Please help.


3 Answers 3

public function log($message) //create function which will create log with date
        $fileName = date('Y-m-d').'_yourlogName.log';
        $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/' .$fileName);
        $logger = new \Zend_Log();
        return $logger->info($message);        

            $this->log("In function==>line number" . __LINE__ ); // add logs
$writer = new \Zend_Log_Writer_Stream(BP . '/var/log/test.log');
$logger = new \Zend_Log();
  • 1
    I used it like this: $this->logger = new \Zend_Log(); $this->logger->addWriter($writer); Then: $this->logger->info('Select Query'. $query1); Its working now. Dec 14, 2022 at 8:37

Magento uses this format From version 2.4.3.

$writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom.log');
$logger = new \Zend_Log();
$logger->info('text message');
$logger->info(print_r($myArray, true));

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.