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?
-
magento.stackexchange.com/a/75954/70343Dominic Pixie– Dominic Pixie2020年06月20日 18:58:36 +00:00Commented Jun 20, 2020 at 18:58
1 Answer 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!
-
Yes, it does help accomplish what I was trying to do. Thank you also for the instructions on doing it in Magento 2.CodeForGood– CodeForGood2020年06月20日 13:27:51 +00:00Commented 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); }CodeForGood– CodeForGood2020年06月22日 14:38:50 +00:00Commented Jun 22, 2020 at 14:38 -
@CodeForGood Not sure about error_log but with file_put_contents it will be generated if not exist!Bhaumik Upadhyay– Bhaumik Upadhyay2020年06月22日 15:41:01 +00:00Commented Jun 22, 2020 at 15:41
-
Thanks, I found out the answer. It will be generated as is the case with file_put_contents.CodeForGood– CodeForGood2020年06月22日 15:46:16 +00:00Commented Jun 22, 2020 at 15:46