From 651e754fdd17c0c8a1bc5d34a6714819d5a335e8 Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Wed, 6 Dec 2023 15:03:22 -0800 Subject: [PATCH 1/5] Show log context as additional togglable table row This is partly based on the work done by @johnregan3 in #31823. Co-authored-by: John Regan --- .../woocommerce/client/legacy/css/admin.scss | 20 ++++++ .../admin/class-wc-admin-log-table-list.php | 65 ++++++++++++++++++- .../views/html-admin-page-status-logs-db.php | 29 +++++++++ .../log-handlers/class-wc-log-handler-db.php | 13 ++-- 4 files changed, 122 insertions(+), 5 deletions(-) diff --git a/plugins/woocommerce/client/legacy/css/admin.scss b/plugins/woocommerce/client/legacy/css/admin.scss index c03168ddcec..64cbdd4fb7c 100644 --- a/plugins/woocommerce/client/legacy/css/admin.scss +++ b/plugins/woocommerce/client/legacy/css/admin.scss @@ -1561,6 +1561,26 @@ table.wc_status_table--tools { .column-source { width: 15%; } + + .column-context { + width: 10%; + } + } + + .column-context { + .button { + span { + line-height: 1.3; + } + } + } + + .log-context { + display: none; + + pre { + white-space: pre-wrap; + } } } diff --git a/plugins/woocommerce/includes/admin/class-wc-admin-log-table-list.php b/plugins/woocommerce/includes/admin/class-wc-admin-log-table-list.php index 2d9b9225632..c9aa8611c1a 100644 --- a/plugins/woocommerce/includes/admin/class-wc-admin-log-table-list.php +++ b/plugins/woocommerce/includes/admin/class-wc-admin-log-table-list.php @@ -92,6 +92,38 @@ class WC_Admin_Log_Table_List extends WP_List_Table { items as $log ) { + $this->single_row( $log ); + $this->context_row( $log ); + } + } + + /** + * Render the additional table row that contains extra log context data. + * + * @param array $log Log entry data. + * + * @return void + */ + protected function context_row( $log ) { + // Maintains alternating row background colors. + ?> + + + +

+
+ + + __( 'Level', 'woocommerce' ), 'message' => __( 'Message', 'woocommerce' ), 'source' => __( 'Source', 'woocommerce' ), + 'context' => __( 'Context', 'woocommerce' ), ); } @@ -180,6 +213,36 @@ class WC_Admin_Log_Table_List extends WP_List_Table { return esc_html( $log['source'] ); } + /** + * Context column. + * + * @param array $log Log entry data. + * + * @return string + */ + public function column_context( $log ) { + $content = ''; + + if ( ! empty( $log['context'] ) ) { + ob_start(); + ?> + + get_items_query_offset(); $query_items = " - SELECT log_id, timestamp, level, message, source + SELECT log_id, timestamp, level, message, source, context FROM {$wpdb->prefix}woocommerce_log {$where} {$order} {$limit} {$offset} "; diff --git a/plugins/woocommerce/includes/admin/views/html-admin-page-status-logs-db.php b/plugins/woocommerce/includes/admin/views/html-admin-page-status-logs-db.php index c465791f07a..594013a104c 100644 --- a/plugins/woocommerce/includes/admin/views/html-admin-page-status-logs-db.php +++ b/plugins/woocommerce/includes/admin/views/html-admin-page-status-logs-db.php @@ -20,6 +20,35 @@ if ( ! defined( 'ABSPATH' ) ) { + getMessage() ); + if ( isset( $context['backtrace'] ) && true === filter_var( $context['backtrace'], FILTER_VALIDATE_BOOLEAN ) ) { + $backtrace = wp_debug_backtrace_summary( self::class, 0, false ); + $context['backtrace'] = array_filter( + $backtrace, + fn( $frame ) => false === strpos( $frame, get_class( wc_get_logger() ) ) + ); } + + $insert['context'] = wp_json_encode( $context, JSON_PRETTY_PRINT ); } return false !== $wpdb->insert( "{$wpdb->prefix}woocommerce_log", $insert, $format ); From 57804c0ed55695cf561e24124350a1da10cf8ff3 Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Wed, 6 Dec 2023 16:05:26 -0800 Subject: [PATCH 2/5] Improve backtrace getter --- .../abstracts/abstract-wc-log-handler.php | 33 +++++++++++++++++++ .../log-handlers/class-wc-log-handler-db.php | 6 +--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/plugins/woocommerce/includes/abstracts/abstract-wc-log-handler.php b/plugins/woocommerce/includes/abstracts/abstract-wc-log-handler.php index 4cf148d172e..036d7bf1a60 100644 --- a/plugins/woocommerce/includes/abstracts/abstract-wc-log-handler.php +++ b/plugins/woocommerce/includes/abstracts/abstract-wc-log-handler.php @@ -54,4 +54,37 @@ abstract class WC_Log_Handler implements WC_Log_Handler_Interface { ) ); } + + /** + * Get a backtrace that shows where the logging function was called. + * + * @return array + */ + protected static function get_backtrace() { + // Get the filenames of the logging-related classes so we can ignore them. + $ignore_files = array_map( + function( $class ) { + try { + $reflector = new \ReflectionClass( $class ); + return $reflector->getFileName(); + } catch ( Exception $exception ) { + return null; + } + }, + array( wc_get_logger(), self::class, static::class ) + ); + + $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace + + $filtered_backtrace = array_filter( + $backtrace, + function( $frame ) use ( $ignore_files ) { + $ignore = isset( $frame['file'] ) && in_array( $frame['file'], $ignore_files, true ); + + return ! $ignore; + } + ); + + return array_values( $filtered_backtrace ); + } } diff --git a/plugins/woocommerce/includes/log-handlers/class-wc-log-handler-db.php b/plugins/woocommerce/includes/log-handlers/class-wc-log-handler-db.php index cdc30fd6150..cfe43487366 100644 --- a/plugins/woocommerce/includes/log-handlers/class-wc-log-handler-db.php +++ b/plugins/woocommerce/includes/log-handlers/class-wc-log-handler-db.php @@ -80,11 +80,7 @@ class WC_Log_Handler_DB extends WC_Log_Handler { unset( $context['source'] ); if ( ! empty( $context ) ) { if ( isset( $context['backtrace'] ) && true === filter_var( $context['backtrace'], FILTER_VALIDATE_BOOLEAN ) ) { - $backtrace = wp_debug_backtrace_summary( self::class, 0, false ); - $context['backtrace'] = array_filter( - $backtrace, - fn( $frame ) => false === strpos( $frame, get_class( wc_get_logger() ) ) - ); + $context['backtrace'] = self::get_backtrace(); } $insert['context'] = wp_json_encode( $context, JSON_PRETTY_PRINT ); From d82747f643ec53ed6bfc64ae8e06d3f5f8b24c7c Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Wed, 6 Dec 2023 16:34:05 -0800 Subject: [PATCH 3/5] Add changelog file --- plugins/woocommerce/changelog/fix-31823-db-logs-context | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 plugins/woocommerce/changelog/fix-31823-db-logs-context diff --git a/plugins/woocommerce/changelog/fix-31823-db-logs-context b/plugins/woocommerce/changelog/fix-31823-db-logs-context new file mode 100644 index 00000000000..97802d6ddac --- /dev/null +++ b/plugins/woocommerce/changelog/fix-31823-db-logs-context @@ -0,0 +1,4 @@ +Significance: minor +Type: update + +Enable viewing context data in the database logger list table From 0d286a19f852985caf49d350fa0f4907841a92bf Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Thu, 7 Dec 2023 10:58:23 -0800 Subject: [PATCH 4/5] Update unit tests to use JSON instead of serialize --- .../legacy/unit-tests/log/log-handler-db.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/plugins/woocommerce/tests/legacy/unit-tests/log/log-handler-db.php b/plugins/woocommerce/tests/legacy/unit-tests/log/log-handler-db.php index 41fb7bb813c..2dcd1738c48 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/log/log-handler-db.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/log/log-handler-db.php @@ -49,63 +49,63 @@ class WC_Tests_Log_Handler_DB extends WC_Unit_Test_Case { 'level' => WC_Log_Levels::get_level_severity( 'debug' ), 'message' => 'msg_debug', 'source' => 'source_debug', - 'context' => serialize( array( 'source' => 'source_debug' ) ), + 'context' => '', ), array( 'timestamp' => $expected_ts, 'level' => WC_Log_Levels::get_level_severity( 'info' ), 'message' => 'msg_info', 'source' => 'source_info', - 'context' => serialize( array( 'source' => 'source_info' ) ), + 'context' => '', ), array( 'timestamp' => $expected_ts, 'level' => WC_Log_Levels::get_level_severity( 'notice' ), 'message' => 'msg_notice', 'source' => 'source_notice', - 'context' => serialize( array( 'source' => 'source_notice' ) ), + 'context' => '', ), array( 'timestamp' => $expected_ts, 'level' => WC_Log_Levels::get_level_severity( 'warning' ), 'message' => 'msg_warning', 'source' => 'source_warning', - 'context' => serialize( array( 'source' => 'source_warning' ) ), + 'context' => '', ), array( 'timestamp' => $expected_ts, 'level' => WC_Log_Levels::get_level_severity( 'error' ), 'message' => 'msg_error', 'source' => 'source_error', - 'context' => serialize( array( 'source' => 'source_error' ) ), + 'context' => '', ), array( 'timestamp' => $expected_ts, 'level' => WC_Log_Levels::get_level_severity( 'critical' ), 'message' => 'msg_critical', 'source' => 'source_critical', - 'context' => serialize( array( 'source' => 'source_critical' ) ), + 'context' => '', ), array( 'timestamp' => $expected_ts, 'level' => WC_Log_Levels::get_level_severity( 'alert' ), 'message' => 'msg_alert', 'source' => 'source_alert', - 'context' => serialize( array( 'source' => 'source_alert' ) ), + 'context' => '', ), array( 'timestamp' => $expected_ts, 'level' => WC_Log_Levels::get_level_severity( 'emergency' ), 'message' => 'msg_emergency', 'source' => 'source_emergency', - 'context' => serialize( array( 'source' => 'source_emergency' ) ), + 'context' => '', ), array( 'timestamp' => $expected_ts, 'level' => WC_Log_Levels::get_level_severity( 'debug' ), 'message' => 'context_test', 'source' => pathinfo( __FILE__, PATHINFO_FILENAME ), - 'context' => serialize( $context ), + 'context' => wp_json_encode( $context, JSON_PRETTY_PRINT ), ), ); From 286fb53418846b44a1f69fd8adf67c65024b97b1 Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Thu, 7 Dec 2023 11:00:56 -0800 Subject: [PATCH 5/5] Only render context row when an entry has context data --- .../includes/admin/class-wc-admin-log-table-list.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/woocommerce/includes/admin/class-wc-admin-log-table-list.php b/plugins/woocommerce/includes/admin/class-wc-admin-log-table-list.php index c9aa8611c1a..07fa5748cb2 100644 --- a/plugins/woocommerce/includes/admin/class-wc-admin-log-table-list.php +++ b/plugins/woocommerce/includes/admin/class-wc-admin-log-table-list.php @@ -100,7 +100,9 @@ class WC_Admin_Log_Table_List extends WP_List_Table { public function display_rows() { foreach ( $this->items as $log ) { $this->single_row( $log ); - $this->context_row( $log ); + if ( ! empty( $log['context'] ) ) { + $this->context_row( $log ); + } } }