diff --git a/plugins/woocommerce/changelog/enhance-improve-log-structure b/plugins/woocommerce/changelog/enhance-improve-log-structure new file mode 100644 index 00000000000..f3fe5a6215f --- /dev/null +++ b/plugins/woocommerce/changelog/enhance-improve-log-structure @@ -0,0 +1,4 @@ +Significance: patch +Type: enhancement + +Improve remote logging structure and content diff --git a/plugins/woocommerce/src/Internal/Logging/RemoteLogger.php b/plugins/woocommerce/src/Internal/Logging/RemoteLogger.php index 6793432c973..19a04d84fec 100644 --- a/plugins/woocommerce/src/Internal/Logging/RemoteLogger.php +++ b/plugins/woocommerce/src/Internal/Logging/RemoteLogger.php @@ -97,8 +97,9 @@ class RemoteLogger extends \WC_Log_Handler { unset( $context['tags'] ); } - if ( isset( $context['error'] ) && is_array( $context['error'] ) && ! empty( $context['error']['file'] ) ) { - $context['error']['file'] = $this->sanitize( $context['error']['file'] ); + if ( isset( $context['error']['file'] ) && is_string( $context['error']['file'] ) && '' !== $context['error']['file'] ) { + $log_data['file'] = $this->sanitize( $context['error']['file'] ); + unset( $context['error']['file'] ); } $extra_attrs = $context['extra'] ?? array(); @@ -371,7 +372,7 @@ class RemoteLogger extends \WC_Log_Handler { * * The trace is sanitized by: * - * 1. Remove the absolute path to the WooCommerce plugin directory. + * 1. Remove the absolute path to the plugin directory based on WC_ABSPATH. This is more accurate than using WP_PLUGIN_DIR when the plugin is symlinked. * 2. Remove the absolute path to the WordPress root directory. * * For example, the trace: @@ -387,12 +388,12 @@ class RemoteLogger extends \WC_Log_Handler { return $message; } - $wc_path = StringUtil::normalize_local_path_slashes( WC_ABSPATH ); - $wp_path = StringUtil::normalize_local_path_slashes( ABSPATH ); + $plugin_path = StringUtil::normalize_local_path_slashes( trailingslashit( dirname( WC_ABSPATH ) ) ); + $wp_path = StringUtil::normalize_local_path_slashes( trailingslashit( ABSPATH ) ); $sanitized = str_replace( - array( $wc_path, $wp_path ), - array( '**/' . dirname( WC_PLUGIN_BASENAME ) . '/', '**/' ), + array( $plugin_path, $wp_path ), + array( './', './' ), $message ); diff --git a/plugins/woocommerce/tests/php/src/Internal/Logging/RemoteLoggerTest.php b/plugins/woocommerce/tests/php/src/Internal/Logging/RemoteLoggerTest.php index 1178a2bf95b..88218f1da75 100644 --- a/plugins/woocommerce/tests/php/src/Internal/Logging/RemoteLoggerTest.php +++ b/plugins/woocommerce/tests/php/src/Internal/Logging/RemoteLoggerTest.php @@ -228,7 +228,7 @@ namespace Automattic\WooCommerce\Tests\Internal\Logging { array( 'feature' => 'woocommerce_core', 'severity' => 'error', - 'message' => 'Fatal error occurred at line 123 in **/wp-content/file.php', + 'message' => 'Fatal error occurred at line 123 in ./wp-content/file.php', 'tags' => array( 'woocommerce', 'php', 'tag1', 'tag2' ), ), ), @@ -236,7 +236,7 @@ namespace Automattic\WooCommerce\Tests\Internal\Logging { 'error', 'Test error message', array( 'backtrace' => ABSPATH . 'wp-content/plugins/woocommerce/file.php' ), - array( 'trace' => '**/woocommerce/file.php' ), + array( 'trace' => './woocommerce/file.php' ), ), 'log with extra attributes' => array( 'error', @@ -254,6 +254,14 @@ namespace Automattic\WooCommerce\Tests\Internal\Logging { ), ), ), + 'log with error file' => array( + 'error', + 'Test error message', + array( 'error' => array( 'file' => WC_ABSPATH . 'includes/class-wc-test.php' ) ), + array( + 'file' => './woocommerce/includes/class-wc-test.php', + ), + ), ); } @@ -536,7 +544,7 @@ namespace Automattic\WooCommerce\Tests\Internal\Logging { */ public function test_sanitize() { $message = WC_ABSPATH . 'includes/class-wc-test.php on line 123'; - $expected = '**/woocommerce/includes/class-wc-test.php on line 123'; + $expected = './woocommerce/includes/class-wc-test.php on line 123'; $result = $this->invoke_private_method( $this->sut, 'sanitize', array( $message ) ); $this->assertEquals( $expected, $result ); } @@ -549,7 +557,7 @@ namespace Automattic\WooCommerce\Tests\Internal\Logging { WC_ABSPATH . 'includes/class-wc-test.php:123', ABSPATH . 'wp-includes/plugin.php:456', ); - $expected = "**/woocommerce/includes/class-wc-test.php:123\n**/wp-includes/plugin.php:456"; + $expected = "./woocommerce/includes/class-wc-test.php:123\n./wp-includes/plugin.php:456"; $result = $this->invoke_private_method( $this->sut, 'sanitize_trace', array( $trace ) ); $this->assertEquals( $expected, $result ); }