0

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();
$this->logger->addWriter($writer);

Guess its not working in 2.4.5 version. Please help.

1

3 Answers 3

1
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();
        $logger->addWriter($writer);
        return $logger->info($message);        
    }


            $this->log("In function==>line number" . __LINE__ ); // add logs
           
2
$writer = new \Zend_Log_Writer_Stream(BP . '/var/log/test.log');
$logger = new \Zend_Log();
$logger->addWriter($writer);
1
  • 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
1

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->addWriter($writer);
$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.