Automattic\WooCommerce\Internal\Logging
RemoteLogger::get_formatted_log()
Get formatted log data to be sent to the remote logging service.
This method formats the log data by sanitizing the message, adding default fields, and including additional context such as backtrace, tags, and extra attributes. It also integrates with WC_Tracks to include blog and store details. The formatted log data is then filtered before being sent to the remote logging service.
Метод класса: RemoteLogger{}
Хуки из метода
Возвращает
Массив
. Formatted log data ready to be sent to the remote logging service.
Использование
$RemoteLogger = new RemoteLogger(); $RemoteLogger->get_formatted_log( $level, $message, $context );
- $level(строка) (обязательный)
- Log level (e.g., 'error', 'warning', 'info').
- $message(строка) (обязательный)
- Log message to be recorded.
- $context(массив)
- Additional information for log handlers, such as 'backtrace', 'tags', 'extra', and 'error'.
По умолчанию: array()
Код RemoteLogger::get_formatted_log() RemoteLogger::get formatted log WC 9.3.3
public function get_formatted_log( $level, $message, $context = array() ) { $log_data = array( // Default fields. 'feature' => 'woocommerce_core', 'severity' => $level, 'message' => $this->sanitize( $message ), 'host' => wp_parse_url( home_url(), PHP_URL_HOST ), 'tags' => array( 'woocommerce', 'php' ), 'properties' => array( 'wc_version' => WC()->version, 'php_version' => phpversion(), 'wp_version' => get_bloginfo( 'version' ), 'request_uri' => $this->sanitize_request_uri( filter_input( INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL ) ), ), ); if ( isset( $context['backtrace'] ) ) { if ( is_array( $context['backtrace'] ) || is_string( $context['backtrace'] ) ) { $log_data['trace'] = $this->sanitize_trace( $context['backtrace'] ); } elseif ( true === $context['backtrace'] ) { $log_data['trace'] = $this->sanitize_trace( self::get_backtrace() ); } unset( $context['backtrace'] ); } if ( isset( $context['tags'] ) && is_array( $context['tags'] ) ) { $log_data['tags'] = array_merge( $log_data['tags'], $context['tags'] ); unset( $context['tags'] ); } if ( class_exists( '\WC_Tracks' ) ) { $user = wp_get_current_user(); $blog_details = \WC_Tracks::get_blog_details( $user->ID ); if ( is_numeric( $blog_details['blog_id'] ) && $blog_details['blog_id'] > 0 ) { $log_data['blog_id'] = $blog_details['blog_id']; } if ( ! empty( $blog_details['store_id'] ) ) { $log_data['properties']['store_id'] = $blog_details['store_id']; } } if ( isset( $context['error'] ) && is_array( $context['error'] ) && ! empty( $context['error']['file'] ) ) { $context['error']['file'] = $this->sanitize( $context['error']['file'] ); } $extra_attrs = $context['extra'] ?? array(); unset( $context['extra'] ); // Merge the extra attributes with the remaining context since we can't send arbitrary fields to Logstash. $log_data['extra'] = array_merge( $extra_attrs, $context ); /** * Filters the formatted log data before sending it to the remote logging service. * Returning a non-array value will prevent the log from being sent. * * @since 9.2.0 * * @param array $log_data The formatted log data. * @param string $level The log level (e.g., 'error', 'warning'). * @param string $message The log message. * @param array $context The original context array. * * @return array The filtered log data. */ return apply_filters( 'woocommerce_remote_logger_formatted_log_data', $log_data, $level, $message, $context ); }