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.
Method of the class: RemoteLogger{}
Hooks from the method
Returns
Array. Formatted log data ready to be sent to the remote logging service.
Usage
$RemoteLogger = new RemoteLogger(); $RemoteLogger->get_formatted_log( $level, $message, $context );
- $level(string) (required)
- Log level (e.g.,
'error','warning', 'info'). - $message(string) (required)
- Log message to be recorded.
- $context(array)
- Additional information for log handlers, such as
'backtrace','tags','extra', and'error'.
Default:array()
RemoteLogger::get_formatted_log() RemoteLogger::get formatted log code WC 10.7.0
public function get_formatted_log( $level, $message, $context = array() ) {
$log_data = array(
// Default fields.
'feature' => 'woocommerce_core',
'severity' => $level,
'message' => $this->sanitize( $message ),
'host' => SafeGlobalFunctionProxy::wp_parse_url( SafeGlobalFunctionProxy::home_url(), PHP_URL_HOST ) ?? 'Unable to retrieve host',
'tags' => array( 'woocommerce', 'php' ),
'properties' => array(
'wc_version' => $this->get_wc_version(),
'php_version' => phpversion(),
'wp_version' => SafeGlobalFunctionProxy::get_bloginfo( 'version' ) ?? 'Unable to retrieve wp version',
'request_uri' => $this->sanitize_request_uri( filter_input( INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL ) ),
'store_id' => SafeGlobalFunctionProxy::get_option( \WC_Install::STORE_ID_OPTION, null ) ?? 'Unable to retrieve store id',
),
);
$blog_id = class_exists( 'Jetpack_Options' ) ? Jetpack_Options::get_option( 'id' ) : null;
if ( ! empty( $blog_id ) && is_int( $blog_id ) ) {
$log_data['blog_id'] = $blog_id;
}
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 ( isset( $context['error']['file'] ) && is_string( $context['error']['file'] ) && '' !== $context['error']['file'] ) {
$log_data['file'] = $this->normalize_paths( $context['error']['file'] );
unset( $context['error']['file'] );
}
$extra_attrs = $context['extra'] ?? array();
unset( $context['extra'] );
unset( $context['remote-logging'] );
// 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 );
}