Automattic\WooCommerce\Internal\PushNotifications\DataStores

StepLogFileReader::parse_line │ public static │ WC 11.3.0

Parses one line as the file handler wrote it: a timestamp, a level, the message, and an optional CONTEXT: JSON block.

A line whose context does not decode is still returned, with the raw text in raw and no context, so a plugin reshaping the line through woocommerce_format_log_entry the row rather than the request.

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

Хуков нет.

Возвращает

array{timestamp: int, level: string, message: string, context: array|null, raw: string|null}|null. Null for a line with no timestamp and level.

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

$result = StepLogFileReader::parse_line( $line ): ?array;
$line(строка) (обязательный)
The line.

Список изменений

С версии 11.3.0 Введена.

Код StepLogFileReader::parse_line() WC 11.3.0

public static function parse_line( string $line ): ?array {
	$line     = rtrim( $line, "\r\n" );
	$segments = explode( ' ', $line, 3 );

	if ( count( $segments ) < 3 ) {
		return null;
	}

	$timestamp = strtotime( $segments[0] );
	$level     = strtolower( $segments[1] );

	if ( false === $timestamp || ! WC_Log_Levels::is_valid_level( $level ) ) {
		return null;
	}

	$chunks  = explode( ' CONTEXT: ', $segments[2], 2 );
	$context = null;
	$raw     = null;

	if ( isset( $chunks[1] ) ) {
		$decoded = json_decode( $chunks[1], true );

		if ( is_array( $decoded ) ) {
			$context = $decoded;
		} else {
			$raw = $line;
		}
	}

	return array(
		'timestamp' => $timestamp,
		'level'     => $level,
		'message'   => $chunks[0],
		'context'   => $context,
		'raw'       => $raw,
	);
}