From 42902ef96df752fd85abef3638d028694592280c Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Fri, 26 Apr 2024 00:12:50 -0700 Subject: [PATCH] Logging: Ensure the woocommerce_format_log_entry filter hook still has access to the log source value (#46851) * Logging: keep full context in woocommerce_format_log_entry filter hook The source value gets removed from the context array before it is converted to JSON and output as part of the log entry. However, this means that the source value is not available when the woocommerce_format_log_entry filter hook fires, so plugins can't easily determine where the log entry came from. This simply ensures that the source value is still available to the filter hook, even though it's not included in the JSON. --- .../changelog/update-log-file-format-source | 4 ++++ .../Internal/Admin/Logging/LogHandlerFileV2.php | 14 ++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 plugins/woocommerce/changelog/update-log-file-format-source diff --git a/plugins/woocommerce/changelog/update-log-file-format-source b/plugins/woocommerce/changelog/update-log-file-format-source new file mode 100644 index 00000000000..8c4693f44f4 --- /dev/null +++ b/plugins/woocommerce/changelog/update-log-file-format-source @@ -0,0 +1,4 @@ +Significance: patch +Type: update + +Ensure the woocommerce_format_log_entry filter hook still has access to the log source value diff --git a/plugins/woocommerce/src/Internal/Admin/Logging/LogHandlerFileV2.php b/plugins/woocommerce/src/Internal/Admin/Logging/LogHandlerFileV2.php index dd509419cf6..19c551c672d 100644 --- a/plugins/woocommerce/src/Internal/Admin/Logging/LogHandlerFileV2.php +++ b/plugins/woocommerce/src/Internal/Admin/Logging/LogHandlerFileV2.php @@ -80,13 +80,15 @@ class LogHandlerFileV2 extends WC_Log_Handler { $time_string = static::format_time( $timestamp ); $level_string = strtoupper( $level ); - unset( $context['source'] ); - if ( ! empty( $context ) ) { - if ( isset( $context['backtrace'] ) && true === filter_var( $context['backtrace'], FILTER_VALIDATE_BOOLEAN ) ) { - $context['backtrace'] = static::get_backtrace(); - } + if ( isset( $context['backtrace'] ) && true === filter_var( $context['backtrace'], FILTER_VALIDATE_BOOLEAN ) ) { + $context['backtrace'] = static::get_backtrace(); + } - $formatted_context = wp_json_encode( $context ); + $context_for_entry = $context; + unset( $context_for_entry['source'] ); + + if ( ! empty( $context_for_entry ) ) { + $formatted_context = wp_json_encode( $context_for_entry ); $message .= " CONTEXT: $formatted_context"; }