format_record_message( $record ); $formatter = $this->get_context_formatter(); } else { // Fall-back on a standard format if the message does not have a context. $formatter = $this->get_standard_formatter(); } return $formatter->format( $record ); } /** * Gets a LineFormatter whose format is context aware. * * @since 4.12.13 * * @return LineFormatter */ public function get_context_formatter() { if ( empty( $this->context_formatter ) ) { $this->context_formatter = new LineFormatter( $this->context_format ); } return $this->context_formatter; } /** * Gets a LineFormatter whose format is our standard logging format. * * @since 4.12.13 * * @return LineFormatter */ public function get_standard_formatter() { if ( empty( $this->standard_formatter ) ) { $this->standard_formatter = new LineFormatter( $this->standard_format ); } return $this->standard_formatter; } /** * Formats a set of log records. * * This simply hands off the work of formatting Batches to the LineFormatter. * * @since 4.12.13 * * @param array $records A set of records to format * @return mixed The formatted set of records */ public function formatBatch( array $records ) { $line_formatter = new LineFormatter(); return $line_formatter->formatBatch( $records ); } /** * Formats the record to the canonical format. * * @since 4.9.16 * * @param array $record The record to process. * * @return string The formatted message, as built from the record context and message, in the format `=`. */ protected function format_record_message( array $record ) { $message = []; $extra = []; $extra['level'] = isset( $record['level_name'] ) ? strtolower( $record['level_name'] ) : 'debug'; if ( ! empty( $record['message'] ) ) { // Use the message as the source. $extra['source'] = $this->escape_quotes( $record['message'] ); } $context = $record['context']; $context = array_merge( $extra, $context ); foreach ( $context as $key => $value ) { $escape = false; if ( is_bool( $value ) ) { $value = $value ? 'true' : 'false'; } elseif ( ! is_scalar( $value ) ) { $value = json_encode( $value ); if ( false === $value ) { $value = 'malformed'; } else { $escape = true; } } if ( $escape || ( is_string( $value ) && preg_match( '~[\\\\/\\s]+~', $value ) ) ) { $value = '"' . $this->escape_quotes( $value ) . '"'; } $message[] = "{$key}={$value}"; } return implode( ' ', $message ); } /** * Escapes the double quotes in a string. * * @since 4.9.16 * * @param string $string The string to escape the quotes in. * * @return string The string, with the quotes escaped. */ protected function escape_quotes( $string ) { return str_replace( '"', '\\"', $string ) ; } }