Improve remote logging structure and content (#51360)

* Refactor RemoteLogger to format error file paths and remove absolute paths

* Add changelog

* Change "**" -> "./"
This commit is contained in:
Chi-Hsuan Huang 2024-09-18 08:58:10 +08:00 committed by GitHub
parent cfca07eca8
commit cf7fd8303c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 11 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: enhancement
Improve remote logging structure and content

View File

@ -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
);

View File

@ -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 );
}