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.
This commit is contained in:
Corey McKrill 2024-04-26 00:12:50 -07:00 committed by GitHub
parent d5d25d3bfe
commit 42902ef96d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 6 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: update
Ensure the woocommerce_format_log_entry filter hook still has access to the log source value

View File

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