0

I have an application developed to manage the orders, vendors and many more things. The default error_log() functionality has already been in use across the application coded in PHP to log not only the errors but order object data to a file called error_log.txt.

I'm looking for a way to log order-specific data to a separate log file so that this data resides in this file as long as I want it to. Could you please guide me in the right direction to achieve this?

CodeForGood
  • 764
  • 7
  • 31

1 Answers1

1

To create the log file in PHP please try the below code:

//Write data to log file
$log  = "Order: ".$orderdata.PHP_EOL."-------------------------".PHP_EOL;

//generate the file file_put_contents('./order_data_'.date('d-M-Y').'.log', $log, FILE_APPEND);

In Magento2, you can create the custom log file using Zend library

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

I hope this helps!

Bhaumik Upadhyay
  • 893
  • 1
  • 9
  • 18
  • Yes, it does help accomplish what I was trying to do. Thank you also for the instructions on doing it in Magento 2. – CodeForGood Jun 20 '20 at 13:27
  • Bhaumik, could you please tell me if the order_data.log doesn't exist in the folder, will it be created by the error_log() in my code snippet. { $log_file = POMS_LOG_FOLDER . "/order_data.log"; $log = $order_message . PHP_EOL . "-------------------------" . PHP_EOL; // logging order info to custom log file error_log($log, 3, $log_file); } – CodeForGood Jun 22 '20 at 14:38
  • @CodeForGood Not sure about error_log but with file_put_contents it will be generated if not exist! – Bhaumik Upadhyay Jun 22 '20 at 15:41
  • Thanks, I found out the answer. It will be generated as is the case with file_put_contents. – CodeForGood Jun 22 '20 at 15:46