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?

asked Jun 20, 2020 at 11:43
1

1 Answer 1

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!

answered Jun 20, 2020 at 13:08
4
  • Yes, it does help accomplish what I was trying to do. Thank you also for the instructions on doing it in Magento 2. Commented Jun 20, 2020 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); } Commented Jun 22, 2020 at 14:38
  • @CodeForGood Not sure about error_log but with file_put_contents it will be generated if not exist! Commented Jun 22, 2020 at 15:41
  • Thanks, I found out the answer. It will be generated as is the case with file_put_contents. Commented Jun 22, 2020 at 15:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.