Automattic\WooCommerce\Internal\Logging

RemoteLogger::logprivateWC 1.0

Send the log to the remote logging service.

Метод класса: RemoteLogger{}

Хуков нет.

Возвращает

true|false.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->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'.

Код RemoteLogger::log() WC 10.3.5

private function log( $level, $message, $context ) {
	$log_data = $this->get_formatted_log( $level, $message, $context );

		// Ensure the log data is valid.
	if ( ! is_array( $log_data ) || empty( $log_data['message'] ) || empty( $log_data['feature'] ) ) {
		return false;
	}

	$body = SafeGlobalFunctionProxy::wp_json_encode( array( 'params' => SafeGlobalFunctionProxy::wp_json_encode( $log_data ) ) );
	if ( is_null( $body ) ) { // if the json encoding fails the API will reject the API call so let's not bother.
		throw new \Error( 'Remote Logger encountered error while attempting to JSON encode $log_data' );
	}

	WC_Rate_Limiter::set_rate_limit( self::RATE_LIMIT_ID, self::RATE_LIMIT_DELAY );

	if ( $this->is_dev_or_local_environment() ) {
		return false;
	}

	$response = SafeGlobalFunctionProxy::wp_safe_remote_post(
		self::LOG_ENDPOINT,
		array(
			'body'     => $body,
			'timeout'  => 3,
			'headers'  => array(
				'Content-Type' => 'application/json',
			),
			'blocking' => false,
		)
	);

	if ( is_null( $response ) ) { // SafeGlobalFunctionProxy will return a null if an error occurs within, so there will be a separate log entry with the details.
		SafeGlobalFunctionProxy::wc_get_logger()->error( 'Failed to call wp_safe_remote_post while sending the log to the remote logging service.', array( 'source' => 'remote-logging' ) );
		return false;
	}

	$is_api_call_error = SafeGlobalFunctionProxy::is_wp_error( $response );

	if ( $is_api_call_error ) {
		SafeGlobalFunctionProxy::wc_get_logger()->error( 'Failed to send the log to the remote logging service: ' . $response->get_error_message(), array( 'source' => 'remote-logging' ) );
		return false;
	} elseif ( is_null( $is_api_call_error ) ) {
		SafeGlobalFunctionProxy::wc_get_logger()->error( 'Failed to parse the response after sending log to the remote logging service. ', array( 'source' => 'remote-logging' ) );
		return false;
	}
	return true;
}