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 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/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/admin/class-wc-admin-log-table-list.php b/plugins/woocommerce/includes/admin/class-wc-admin-log-table-list.php index 2d9b9225632..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 @@ -92,6 +92,40 @@ class WC_Admin_Log_Table_List extends WP_List_Table { items as $log ) { + $this->single_row( $log ); + if ( ! empty( $log['context'] ) ) { + $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 +215,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 ) ) { + $context['backtrace'] = self::get_backtrace(); } + + $insert['context'] = wp_json_encode( $context, JSON_PRETTY_PRINT ); } return false !== $wpdb->insert( "{$wpdb->prefix}woocommerce_log", $insert, $format ); 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 ), ), );