From fb4b145b8a3c9dc949dbee85e08e98d97ceb1719 Mon Sep 17 00:00:00 2001 From: vinkmeta Date: Wed, 11 Jun 2025 13:05:25 +0100 Subject: [PATCH 1/6] Removed unnecessary logs --- facebook-commerce.php | 21 ++------------------- includes/fbproduct.php | 3 --- 2 files changed, 2 insertions(+), 22 deletions(-) diff --git a/facebook-commerce.php b/facebook-commerce.php index 220c4fa5b..ec811711d 100644 --- a/facebook-commerce.php +++ b/facebook-commerce.php @@ -1335,23 +1335,6 @@ public function update_product_group( WC_Facebook_Product $woo_product ) { $variants = $woo_product->prepare_variants_for_group(); if ( ! $variants ) { - Logger::log( - sprintf( - /* translators: %1$s is referring to facebook product group id. */ - __( - 'Nothing to update for product group for %1$s', - 'facebook-for-woocommerce' - ), - $fb_product_group_id - ), - [], - array( - 'should_send_log_to_meta' => false, - 'should_save_log_in_woocommerce' => true, - 'woocommerce_log_level' => \WC_Log_Levels::DEBUG, - ) - ); - return; } @@ -2177,7 +2160,7 @@ private function sync_facebook_products_using_background_processor() { array( 'should_send_log_to_meta' => false, 'should_save_log_in_woocommerce' => true, - 'woocommerce_log_level' => \WC_Log_Levels::DEBUG, + 'woocommerce_log_level' => \WC_Log_Levels::WARNING, ) ); throw new PluginException( __( 'The plugin is not configured or the Catalog ID is missing.', 'facebook-for-woocommerce' ) ); @@ -2224,7 +2207,7 @@ private function sync_facebook_products_using_background_processor() { array( 'should_send_log_to_meta' => false, 'should_save_log_in_woocommerce' => true, - 'woocommerce_log_level' => \WC_Log_Levels::DEBUG, + 'woocommerce_log_level' => \WC_Log_Levels::WARNING, ) ); WC_Facebookcommerce_Utils::fblog( diff --git a/includes/fbproduct.php b/includes/fbproduct.php index 592d2e807..92baa272b 100644 --- a/includes/fbproduct.php +++ b/includes/fbproduct.php @@ -2107,8 +2107,6 @@ public function prepare_variants_for_item( &$product_data ) { break; }//end switch } else { - - \WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( $product->get_id() . ': No options for ' . $original_variant_name ); continue; }//end if }//end foreach @@ -2162,7 +2160,6 @@ public function prepare_variants_for_group( $feed_data = false ) { $option_values = $variation_attributes[ $key ]; } else { // skip variations without valid attribute options - \WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( $product->get_id() . ': No options for ' . $name ); continue; } From de419a5d01bc5e1428c3cdfa758d33148fc69e0e Mon Sep 17 00:00:00 2001 From: vinkmeta Date: Wed, 11 Jun 2025 13:15:47 +0100 Subject: [PATCH 2/6] Removed duplicate loggers --- includes/Framework/ErrorLogHandler.php | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/includes/Framework/ErrorLogHandler.php b/includes/Framework/ErrorLogHandler.php index 1db527606..0aef997cc 100644 --- a/includes/Framework/ErrorLogHandler.php +++ b/includes/Framework/ErrorLogHandler.php @@ -49,13 +49,27 @@ public function process_error_log( $raw_context ) { $context = self::set_core_log_context( $raw_context ); try { $response = facebook_for_woocommerce()->get_api()->log_to_meta( $context ); - if ( $response->success ) { - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( 'Error log: ' . wp_json_encode( $context ), \WC_Log_Levels::ERROR ); - } else { - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( 'Bad response from log_to_meta request', \WC_Log_Levels::ERROR ); + if ( ! $response->success ) { + Logger::log( + 'Bad response from log_to_meta request', + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::ERROR, + ) + ); } } catch ( \Exception $e ) { - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( 'Error persisting error logs: ' . $e->getMessage(), \WC_Log_Levels::ERROR ); + Logger::log( + 'Error persisting error logs: ' . $e->getMessage(), + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::ERROR, + ) + ); } } From f38d91c1e3222e30b2d650424f997125c64d95ea Mon Sep 17 00:00:00 2001 From: vinkmeta Date: Wed, 11 Jun 2025 06:43:37 -0700 Subject: [PATCH 3/6] Deprecated log_to_meta functionality completely (#3336) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: ## Description **Issues** The current logging architecture within our plugin is disjointed, with multiple logging mechanisms in place, leading to inefficiencies and redundancies: - Batched Log Transmission to Meta Server: - We recently rolled out process to synchronize logs with the Meta server. However, it also redundantly logs the same information to the advertiser's server. Many of these logs are not pertinent to the advertiser's debugging needs. - Furthermore, the logs sent to the advertiser's server include a plethora of static configuration details, such as commerce_merchant_settings_id, commerce_partner_integration_id, external_business_id, and catalog_id. This results in log saturation, as these details are logged with every entry, offering no substantial value and taking advertiser's server memory. - Additionally, if the Meta logging endpoint encounters an exception, it perpetuates a uniform error message for every log batch it attempts to transmit, potentially leading to hundreds of identical error messages. - In scenarios where exceptions introduce null values into the global message queue, the logging process can become permanently disrupted, failing to process subsequent error logs—an edge case that has not been adequately addressed. - Moreover, all logs sent to the server are currently categorized under the 'Notice' log level, whereas they should be classified as 'Debug' to reflect their nature accurately. - WooCommerce Server Logging Issues: - Logs are uniformly recorded at the 'Notice' level, which is inappropriate. Ideally, logs should be categorized as 'Debug', 'Warning', or 'Error', depending on their severity. - The creation of multiple log IDs has resulted in the generation of separate log files for different log types, unnecessarily complicating log management. - Additionally, the logging of Items Batch API calls is overwhelming the server logs. - Many logs contain a static error message indicating that the EMS is missing, which is inaccurate, as EMS should never be null once the connection is correctly established. - Plugin Version Logging: - A distinct API endpoint is invoked daily to log the plugin version and its configuration. - This is redundant, as separate APIs are not required to persist this information. - Tip Event Logging: - Separate logging APIs are triggered for tip events associated with banners, which is also unnecessary. **Solution** To address these issues, we need to implement a centralized logging system. This system should efficiently manage log transmission to the Meta server through a single API and also persist logs in WooCommerce servers applying appropriate log levels. All existing logs should be migrated to utilize this centralized logger. **This PR** Deprecated log_to_meta functionality completely ### Type of change - Fix (non-breaking change which fixes an issue) ## Checklist - [x] I have commented my code, particularly in hard-to-understand areas, if any. - [x] I have confirmed that my changes do not introduce any new PHPCS warnings or errors. - [x] I have checked plugin debug logs that my changes do not introduce any new PHP warnings or FATAL errors. - [x] I followed general Pull Request best practices. Meta employees to follow this [wiki]([url](https://fburl.com/wiki/2cgfduwc)). - [x] I have added tests (if necessary) and all the new and existing unit tests pass locally with my changes. - [x] I have completed dogfooding and QA testing, or I have conducted thorough due diligence to ensure that it does not break existing functionality. - [x] I have updated or requested update to plugin documentations (if necessary). Meta employees to follow this [wiki]([url](https://fburl.com/wiki/nhx73tgs)). ## Changelog entry Fixed an edge case with BatchLogHandler Pull Request resolved: https://github.com/facebook/facebook-for-woocommerce/pull/3336 Reviewed By: SayanPandey Differential Revision: D76420187 Pulled By: vinkmeta fbshipit-source-id: 8dfa5bbabb96065395afc8b0a1a6be4d20a79765 --- includes/fbutils.php | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/includes/fbutils.php b/includes/fbutils.php index 995096183..540efc748 100644 --- a/includes/fbutils.php +++ b/includes/fbutils.php @@ -991,28 +991,6 @@ public static function log_exception_immediately_to_meta( Throwable $error, arra ErrorLogHandler::log_exception_to_meta( $error, $context ); } - /** - * Utility function for sending logs to Meta. - * - * @since 3.5.0 - * - * @param string $message - * @param array $context optional error message attributes - */ - public static function log_to_meta( string $message, array $context = [] ) { - $extra_data = self::get_context_data( $context, 'extra_data', [] ); - $extra_data['message'] = $message; - $context['extra_data'] = $extra_data; - - // Push logging request to global message queue function. - $logs = get_transient( 'global_logging_message_queue' ); - if ( ! $logs ) { - $logs = []; - } - $logs[] = $context; - set_transient( 'global_logging_message_queue', $logs, HOUR_IN_SECONDS ); - } - /** * Checks whether fpassthru has been disabled in PHP. * From 3400da89abcda605455b552390c86aab0b243e3c Mon Sep 17 00:00:00 2001 From: vinkmeta Date: Wed, 11 Jun 2025 06:46:42 -0700 Subject: [PATCH 4/6] Migrated Feed Logger (#3340) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: ## Description **Issues** The current logging architecture within our plugin is disjointed, with multiple logging mechanisms in place, leading to inefficiencies and redundancies: - Batched Log Transmission to Meta Server: - We recently rolled out process to synchronize logs with the Meta server. However, it also redundantly logs the same information to the advertiser's server. Many of these logs are not pertinent to the advertiser's debugging needs. - Furthermore, the logs sent to the advertiser's server include a plethora of static configuration details, such as commerce_merchant_settings_id, commerce_partner_integration_id, external_business_id, and catalog_id. This results in log saturation, as these details are logged with every entry, offering no substantial value and taking advertiser's server memory. - Additionally, if the Meta logging endpoint encounters an exception, it perpetuates a uniform error message for every log batch it attempts to transmit, potentially leading to hundreds of identical error messages. - In scenarios where exceptions introduce null values into the global message queue, the logging process can become permanently disrupted, failing to process subsequent error logs—an edge case that has not been adequately addressed. - Moreover, all logs sent to the server are currently categorized under the 'Notice' log level, whereas they should be classified as 'Debug' to reflect their nature accurately. - WooCommerce Server Logging Issues: - Logs are uniformly recorded at the 'Notice' level, which is inappropriate. Ideally, logs should be categorized as 'Debug', 'Warning', or 'Error', depending on their severity. - The creation of multiple log IDs has resulted in the generation of separate log files for different log types, unnecessarily complicating log management. - Additionally, the logging of Items Batch API calls is overwhelming the server logs. - Many logs contain a static error message indicating that the EMS is missing, which is inaccurate, as EMS should never be null once the connection is correctly established. - Plugin Version Logging: - A distinct API endpoint is invoked daily to log the plugin version and its configuration. - This is redundant, as separate APIs are not required to persist this information. - Tip Event Logging: - Separate logging APIs are triggered for tip events associated with banners, which is also unnecessary. **Solution** To address these issues, we need to implement a centralized logging system. This system should efficiently manage log transmission to the Meta server through a single API and also persist logs in WooCommerce servers applying appropriate log levels. All existing logs should be migrated to utilize this centralized logger. **This PR** Migrated Feed Logger ### Type of change - Fix (non-breaking change which fixes an issue) ## Checklist - [x] I have commented my code, particularly in hard-to-understand areas, if any. - [x] I have confirmed that my changes do not introduce any new PHPCS warnings or errors. - [x] I have checked plugin debug logs that my changes do not introduce any new PHP warnings or FATAL errors. - [x] I followed general Pull Request best practices. Meta employees to follow this [wiki]([url](https://fburl.com/wiki/2cgfduwc)). - [x] I have added tests (if necessary) and all the new and existing unit tests pass locally with my changes. - [x] I have completed dogfooding and QA testing, or I have conducted thorough due diligence to ensure that it does not break existing functionality. - [x] I have updated or requested update to plugin documentations (if necessary). Meta employees to follow this [wiki]([url](https://fburl.com/wiki/nhx73tgs)). ## Changelog entry Migrated Feed Logger Pull Request resolved: https://github.com/facebook/facebook-for-woocommerce/pull/3340 Test Plan: Change in Logging npm run test:php Reviewed By: SayanPandey Differential Revision: D76422215 Pulled By: vinkmeta fbshipit-source-id: bf7cd68018a9b274b228dc6387efce7444096dc5 --- includes/ExternalVersionUpdate/Update.php | 11 ++- includes/Products/Feed.php | 82 ++++++++++++++++++++--- 2 files changed, 83 insertions(+), 10 deletions(-) diff --git a/includes/ExternalVersionUpdate/Update.php b/includes/ExternalVersionUpdate/Update.php index 0f08e34b7..78b9f124e 100644 --- a/includes/ExternalVersionUpdate/Update.php +++ b/includes/ExternalVersionUpdate/Update.php @@ -15,6 +15,7 @@ use Exception; use WC_Facebookcommerce_Utils; use WooCommerce\Facebook\Utilities\Heartbeat; +use WooCommerce\Facebook\Framework\Logger; /** * Facebook for WooCommerce External Plugin Version Update. @@ -90,7 +91,15 @@ public function send_new_version_to_facebook_server() { } return update_option( self::LATEST_VERSION_SENT, WC_Facebookcommerce_Utils::PLUGIN_VERSION ); } catch ( Exception $e ) { - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( $e->getMessage() ); + Logger::log( + $e->getMessage(), + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::ERROR, + ) + ); // If the request fails, we should retry it in the next heartbeat. return false; } diff --git a/includes/Products/Feed.php b/includes/Products/Feed.php index f0692293b..dc2d33407 100644 --- a/includes/Products/Feed.php +++ b/includes/Products/Feed.php @@ -82,7 +82,15 @@ private function add_hooks() { * @throws PluginException If the feed secret is invalid, file is not readable, or other errors occur. */ public function handle_feed_data_request() { - \WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( 'Facebook is requesting the product feed.' ); + Logger::log( + 'Facebook is requesting the product feed.', + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::DEBUG, + ) + ); facebook_for_woocommerce()->get_tracker()->track_feed_file_requested(); $feed_handler = new \WC_Facebook_Product_Feed(); @@ -120,7 +128,15 @@ public function handle_feed_data_request() { // fpassthru might be disabled in some hosts (like Flywheel) if ( $this->is_fpassthru_disabled() || ! @fpassthru( $file ) ) { - \WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( 'fpassthru is disabled: getting file contents' ); + Logger::log( + 'fpassthru is disabled: getting file contents', + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::DEBUG, + ) + ); $contents = @stream_get_contents( $file ); if ( ! $contents ) { throw new PluginException( 'Could not get feed file contents.', 500 ); @@ -128,7 +144,15 @@ public function handle_feed_data_request() { echo $contents; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } } catch ( \Exception $exception ) { - \WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( 'Could not serve product feed. ' . $exception->getMessage() . ' (' . $exception->getCode() . ')' ); + Logger::log( + 'Could not serve product feed. ' . $exception->getMessage() . ' (' . $exception->getCode() . ')', + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::ERROR, + ) + ); status_header( $exception->getCode() ); } exit; @@ -189,7 +213,7 @@ public function schedule_feed_generation() { array( 'should_send_log_to_meta' => true, 'should_save_log_in_woocommerce' => true, - 'woocommerce_log_level' => \WC_Log_Levels::DEBUG, + 'woocommerce_log_level' => \WC_Log_Levels::WARNING, ) ); return; @@ -218,7 +242,15 @@ public function schedule_feed_generation() { public function send_request_to_upload_feed() { $feed_id = self::retrieve_or_create_integration_feed_id(); if ( empty( $feed_id ) ) { - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( 'Feed: integration feed ID is null or empty, feed will not be uploaded.' ); + Logger::log( + 'Feed: integration feed ID is null or empty, feed will not be uploaded.', + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::WARNING, + ) + ); return; } @@ -245,7 +277,15 @@ public function retrieve_or_create_integration_feed_id() { $feed_id = self::request_and_filter_integration_feed_id(); if ( $feed_id ) { facebook_for_woocommerce()->get_integration()->update_feed_id( $feed_id ); - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( 'Feed: feed_id = ' . $feed_id . ', queried and selected from Meta API.' ); + Logger::log( + 'Feed: feed_id = ' . $feed_id . ', queried and selected from Meta API.', + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::DEBUG, + ) + ); return $feed_id; } @@ -253,7 +293,15 @@ public function retrieve_or_create_integration_feed_id() { $feed_id = self::create_feed_id(); if ( $feed_id ) { facebook_for_woocommerce()->get_integration()->update_feed_id( $feed_id ); - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( 'Feed: feed_id = ' . $feed_id . ', created a new feed via Meta API.' ); + Logger::log( + 'Feed: feed_id = ' . $feed_id . ', created a new feed via Meta API.', + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::DEBUG, + ) + ); return $feed_id; } @@ -278,7 +326,15 @@ private function request_and_filter_integration_feed_id() { $feed_nodes = facebook_for_woocommerce()->get_api()->read_feeds( $catalog_id )->data; } catch ( Exception $e ) { $message = sprintf( 'There was an error trying to get feed nodes for catalog: %s', $e->getMessage() ); - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( $message ); + Logger::log( + $message, + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::ERROR, + ) + ); return ''; } @@ -298,7 +354,15 @@ private function request_and_filter_integration_feed_id() { $feed_metadata = facebook_for_woocommerce()->get_api()->read_feed( $feed['id'] ); } catch ( Exception $e ) { $message = sprintf( 'There was an error trying to get feed metadata: %s', $e->getMessage() ); - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( $message ); + Logger::log( + $message, + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::ERROR, + ) + ); continue; } From f5aa852e96c5f7a1f14cb0f817a0d67ea2170a9c Mon Sep 17 00:00:00 2001 From: vinkmeta Date: Wed, 11 Jun 2025 06:46:48 -0700 Subject: [PATCH 5/6] Updated exception logs (#3338) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: ## Description **Issues** The current logging architecture within our plugin is disjointed, with multiple logging mechanisms in place, leading to inefficiencies and redundancies: - Batched Log Transmission to Meta Server: - We recently rolled out process to synchronize logs with the Meta server. However, it also redundantly logs the same information to the advertiser's server. Many of these logs are not pertinent to the advertiser's debugging needs. - Furthermore, the logs sent to the advertiser's server include a plethora of static configuration details, such as commerce_merchant_settings_id, commerce_partner_integration_id, external_business_id, and catalog_id. This results in log saturation, as these details are logged with every entry, offering no substantial value and taking advertiser's server memory. - Additionally, if the Meta logging endpoint encounters an exception, it perpetuates a uniform error message for every log batch it attempts to transmit, potentially leading to hundreds of identical error messages. - In scenarios where exceptions introduce null values into the global message queue, the logging process can become permanently disrupted, failing to process subsequent error logs—an edge case that has not been adequately addressed. - Moreover, all logs sent to the server are currently categorized under the 'Notice' log level, whereas they should be classified as 'Debug' to reflect their nature accurately. - WooCommerce Server Logging Issues: - Logs are uniformly recorded at the 'Notice' level, which is inappropriate. Ideally, logs should be categorized as 'Debug', 'Warning', or 'Error', depending on their severity. - The creation of multiple log IDs has resulted in the generation of separate log files for different log types, unnecessarily complicating log management. - Additionally, the logging of Items Batch API calls is overwhelming the server logs. - Many logs contain a static error message indicating that the EMS is missing, which is inaccurate, as EMS should never be null once the connection is correctly established. - Plugin Version Logging: - A distinct API endpoint is invoked daily to log the plugin version and its configuration. - This is redundant, as separate APIs are not required to persist this information. - Tip Event Logging: - Separate logging APIs are triggered for tip events associated with banners, which is also unnecessary. **Solution** To address these issues, we need to implement a centralized logging system. This system should efficiently manage log transmission to the Meta server through a single API and also persist logs in WooCommerce servers applying appropriate log levels. All existing logs should be migrated to utilize this centralized logger. **This PR** Migrating exception logs to centralised logger ### Type of change - Fix (non-breaking change which fixes an issue) ## Checklist - [x] I have commented my code, particularly in hard-to-understand areas, if any. - [x] I have confirmed that my changes do not introduce any new PHPCS warnings or errors. - [x] I have checked plugin debug logs that my changes do not introduce any new PHP warnings or FATAL errors. - [x] I followed general Pull Request best practices. Meta employees to follow this [wiki]([url](https://fburl.com/wiki/2cgfduwc)). - [x] I have added tests (if necessary) and all the new and existing unit tests pass locally with my changes. - [x] I have completed dogfooding and QA testing, or I have conducted thorough due diligence to ensure that it does not break existing functionality. - [x] I have updated or requested update to plugin documentations (if necessary). Meta employees to follow this [wiki]([url](https://fburl.com/wiki/nhx73tgs)). ## Changelog entry Migrating exception logs to centralised logger Pull Request resolved: https://github.com/facebook/facebook-for-woocommerce/pull/3338 Test Plan: Change in Logging npm run test:php Reviewed By: SayanPandey Differential Revision: D76422202 Pulled By: vinkmeta fbshipit-source-id: 05da6d1d98e42f9da308edcfc7c760ea4341abd0 --- facebook-commerce.php | 134 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 118 insertions(+), 16 deletions(-) diff --git a/facebook-commerce.php b/facebook-commerce.php index ec811711d..fbace4002 100644 --- a/facebook-commerce.php +++ b/facebook-commerce.php @@ -1310,7 +1310,15 @@ public function create_product_group( WC_Facebook_Product $woo_product, string $ } } catch ( ApiException $e ) { $message = sprintf( 'There was an error trying to create the product group: %s', $e->getMessage() ); - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( $message ); + Logger::log( + $message, + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::ERROR, + ) + ); } return null; @@ -1366,7 +1374,15 @@ public function update_product_group( WC_Facebook_Product $woo_product ) { } } catch ( ApiException $e ) { $message = sprintf( 'There was an error trying to update Product Group %s: %s', $fb_product_group_id, $e->getMessage() ); - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( $message ); + Logger::log( + $message, + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::ERROR, + ) + ); } } @@ -1394,7 +1410,15 @@ public function create_product_item_batch_api( $woo_product, $retailer_id ): str } } catch ( ApiException $e ) { $message = sprintf( 'There was an error trying to create a product item: %s', $e->getMessage() ); - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( $message ); + Logger::log( + $message, + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::ERROR, + ) + ); } return ''; @@ -1424,7 +1448,15 @@ public function create_product_item( $woo_product, $retailer_id, $product_group_ } } catch ( ApiException $e ) { $message = sprintf( 'There was an error trying to create a product item: %s', $e->getMessage() ); - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( $message ); + Logger::log( + $message, + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::ERROR, + ) + ); } return ''; @@ -1556,7 +1588,15 @@ public function update_product_item_batch_api( WC_Facebook_Product $woo_product, } } catch ( ApiException $e ) { $message = sprintf( 'There was an error trying to update a product item: %s', $e->getMessage() ); - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( $message ); + Logger::log( + $message, + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::ERROR, + ) + ); } } @@ -1590,7 +1630,15 @@ public function update_product_item( WC_Facebook_Product $woo_product, string $f } } catch ( ApiException $e ) { $message = sprintf( 'There was an error trying to update a product item: %s', $e->getMessage() ); - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( $message ); + Logger::log( + $message, + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::ERROR, + ) + ); } } @@ -1623,7 +1671,15 @@ public function create_or_update_product_set_item( $product_set_data, $product_s } } catch ( ApiException $e ) { $message = sprintf( 'There was an error trying to create/update a product set: %s', $e->getMessage() ); - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( $message ); + Logger::log( + $message, + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::ERROR, + ) + ); } } @@ -1642,7 +1698,15 @@ public function delete_product_set_item( string $fb_product_set_id ) { $this->facebook_for_woocommerce->get_api()->delete_product_set_item( $fb_product_set_id, $allow_live_deletion ); } catch ( ApiException $e ) { $message = sprintf( 'There was an error trying to delete a product set item: %s', $e->getMessage() ); - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( $message ); + Logger::log( + $message, + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::ERROR, + ) + ); } } @@ -1836,7 +1900,15 @@ public function remove_resync_message() { * @return void */ public function display_error_message( string $msg ): void { - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( $msg ); + Logger::log( + $msg, + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::ERROR, + ) + ); set_transient( 'facebook_plugin_api_error', $msg, self::FB_MESSAGE_DISPLAY_TIME ); } @@ -2198,7 +2270,15 @@ private function sync_facebook_products_using_background_processor() { $catalog = $this->facebook_for_woocommerce->get_api()->get_catalog( $this->get_product_catalog_id() ); } catch ( ApiException $e ) { $message = sprintf( 'There was an error trying to delete a product set item: %s', $e->getMessage() ); - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( $message ); + Logger::log( + $message, + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::ERROR, + ) + ); } if ( $catalog->id ) { Logger::log( @@ -2839,11 +2919,18 @@ public function delete_product_item( int $wp_id ): void { ); if ( $fb_product_item_id ) { try { - $pi_result = $this->facebook_for_woocommerce->get_api()->delete_product_item( $fb_product_item_id ); - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( $pi_result ); + $this->facebook_for_woocommerce->get_api()->delete_product_item( $fb_product_item_id ); } catch ( ApiException $e ) { $message = sprintf( 'There was an error trying to delete a product set item: %s', $e->getMessage() ); - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( $message ); + Logger::log( + $message, + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::ERROR, + ) + ); } } } @@ -2916,7 +3003,15 @@ public function update_fb_visibility( $product_id, $visibility ) { } } catch ( ApiException $e ) { $message = sprintf( 'There was an error trying to update product item: %s', $e->getMessage() ); - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( $message ); + Logger::log( + $message, + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::ERROR, + ) + ); } } } @@ -2990,8 +3085,15 @@ public function get_product_fbid( string $fbid_type, int $wp_id, $woo_product = return $fb_id; } } catch ( Exception $e ) { - /* @TODO: Log exception. */ - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( $e->getMessage() ); + Logger::log( + 'There was an issue connecting to the Facebook API:' . $e->getMessage(), + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::ERROR, + ) + ); $this->display_error_message( sprintf( /* translators: Placeholders %1$s - original error message from Facebook API */ From 872ea4e31d87f749bde22752bd8ee1bba60f57d2 Mon Sep 17 00:00:00 2001 From: vinkmeta Date: Wed, 11 Jun 2025 06:47:05 -0700 Subject: [PATCH 6/6] Removed duplicate logging (#3337) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: ## Description **Issues** The current logging architecture within our plugin is disjointed, with multiple logging mechanisms in place, leading to inefficiencies and redundancies: - Batched Log Transmission to Meta Server: - We recently rolled out process to synchronize logs with the Meta server. However, it also redundantly logs the same information to the advertiser's server. Many of these logs are not pertinent to the advertiser's debugging needs. - Furthermore, the logs sent to the advertiser's server include a plethora of static configuration details, such as commerce_merchant_settings_id, commerce_partner_integration_id, external_business_id, and catalog_id. This results in log saturation, as these details are logged with every entry, offering no substantial value and taking advertiser's server memory. - Additionally, if the Meta logging endpoint encounters an exception, it perpetuates a uniform error message for every log batch it attempts to transmit, potentially leading to hundreds of identical error messages. - In scenarios where exceptions introduce null values into the global message queue, the logging process can become permanently disrupted, failing to process subsequent error logs—an edge case that has not been adequately addressed. - Moreover, all logs sent to the server are currently categorized under the 'Notice' log level, whereas they should be classified as 'Debug' to reflect their nature accurately. - WooCommerce Server Logging Issues: - Logs are uniformly recorded at the 'Notice' level, which is inappropriate. Ideally, logs should be categorized as 'Debug', 'Warning', or 'Error', depending on their severity. - The creation of multiple log IDs has resulted in the generation of separate log files for different log types, unnecessarily complicating log management. - Additionally, the logging of Items Batch API calls is overwhelming the server logs. - Many logs contain a static error message indicating that the EMS is missing, which is inaccurate, as EMS should never be null once the connection is correctly established. - Plugin Version Logging: - A distinct API endpoint is invoked daily to log the plugin version and its configuration. - This is redundant, as separate APIs are not required to persist this information. - Tip Event Logging: - Separate logging APIs are triggered for tip events associated with banners, which is also unnecessary. **Solution** To address these issues, we need to implement a centralized logging system. This system should efficiently manage log transmission to the Meta server through a single API and also persist logs in WooCommerce servers applying appropriate log levels. All existing logs should be migrated to utilize this centralized logger. **This PR** Removed duplicate logging ### Type of change - Fix (non-breaking change which fixes an issue) ## Checklist - [x] I have commented my code, particularly in hard-to-understand areas, if any. - [x] I have confirmed that my changes do not introduce any new PHPCS warnings or errors. - [x] I have checked plugin debug logs that my changes do not introduce any new PHP warnings or FATAL errors. - [x] I followed general Pull Request best practices. Meta employees to follow this [wiki]([url](https://fburl.com/wiki/2cgfduwc)). - [x] I have added tests (if necessary) and all the new and existing unit tests pass locally with my changes. - [x] I have completed dogfooding and QA testing, or I have conducted thorough due diligence to ensure that it does not break existing functionality. - [x] I have updated or requested update to plugin documentations (if necessary). Meta employees to follow this [wiki]([url](https://fburl.com/wiki/nhx73tgs)). ## Changelog entry Removed duplicate logging Pull Request resolved: https://github.com/facebook/facebook-for-woocommerce/pull/3337 Test Plan: Change in Logging npm run test:php Reviewed By: SayanPandey Differential Revision: D76420434 Pulled By: vinkmeta fbshipit-source-id: e8bae3fba854d80603bc05b82c8271e5b1ce4d8e --- includes/Framework/BatchLogHandler.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/includes/Framework/BatchLogHandler.php b/includes/Framework/BatchLogHandler.php index 786d8e712..5e0d890fc 100644 --- a/includes/Framework/BatchLogHandler.php +++ b/includes/Framework/BatchLogHandler.php @@ -68,14 +68,29 @@ function ( $log ) { try { $response = facebook_for_woocommerce()->get_api()->log_to_meta( $context ); if ( $response->success ) { - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( 'Meta logs: ' . wp_json_encode( $context ) ); return []; } else { - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( 'Bad response from log_to_meta request' ); + Logger::log( + 'Bad response from Meta logging APIs', + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::ERROR, + ) + ); return $logs_chunk; } } catch ( \Exception $e ) { - WC_Facebookcommerce_Utils::log_with_debug_mode_enabled( 'Error persisting meta logs: ' . $e->getMessage() ); + Logger::log( + 'Error persisting Meta logs: ' . $e->getMessage(), + [], + array( + 'should_send_log_to_meta' => false, + 'should_save_log_in_woocommerce' => true, + 'woocommerce_log_level' => \WC_Log_Levels::ERROR, + ) + ); return $logs_chunk; } },