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

문제

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?

도움이 되었습니까?

해결책

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!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top