PHP 7: How to create a separate log file in PHP to log only the order data(Order info retrieved from an Magento Order object)?

magento.stackexchange https://magento.stackexchange.com/questions/315533

Question

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?

Was it helpful?

Solution

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top