From 50f6db27049cb02cdffa2bd643bb486403ce7e4e Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 8 Dec 2021 11:24:30 +0000 Subject: [PATCH] Replace both EOL and control characters --- .../core/log/LogFormatUtils.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/log/LogFormatUtils.java b/spring-core/src/main/java/org/springframework/core/log/LogFormatUtils.java index e3ae033e9f2..9a2693e2ce6 100644 --- a/spring-core/src/main/java/org/springframework/core/log/LogFormatUtils.java +++ b/spring-core/src/main/java/org/springframework/core/log/LogFormatUtils.java @@ -17,6 +17,7 @@ package org.springframework.core.log; import java.util.function.Function; +import java.util.regex.Pattern; import org.apache.commons.logging.Log; @@ -35,9 +36,15 @@ import org.springframework.lang.Nullable; */ public abstract class LogFormatUtils { + private static final Pattern NEWLINE_PATTERN = Pattern.compile("[\n\r]"); + + private static final Pattern CONTROL_CHARACTER_PATTERN = Pattern.compile("\\p{Cc}"); + + /** - * Variant of {@link #formatValue(Object, int, boolean)} and a convenience - * method that truncates at 100 characters when {@code limitLength} is set. + * Convenience variant of {@link #formatValue(Object, int, boolean)} that + * limits the length of a log message to 100 characters and also replaces + * newline and control characters if {@code limitLength} is set to "true". * @param value the value to format * @param limitLength whether to truncate the value at a length of 100 * @return the formatted value @@ -52,10 +59,13 @@ public abstract class LogFormatUtils { * compacting it into a single line when {@code replaceNewLines} is set. * @param value the value to be formatted * @param maxLength the max length, after which to truncate, or -1 for unlimited - * @param replaceNewlines whether to replace newline characters with placeholders + * @param replaceNewlinesAndControlCharacters whether to replace newline and + * control characters with placeholders * @return the formatted value */ - public static String formatValue(@Nullable Object value, int maxLength, boolean replaceNewlines) { + public static String formatValue( + @Nullable Object value, int maxLength, boolean replaceNewlinesAndControlCharacters) { + if (value == null) { return ""; } @@ -69,8 +79,9 @@ public abstract class LogFormatUtils { if (maxLength != -1) { result = (result.length() > maxLength ? result.substring(0, maxLength) + " (truncated)..." : result); } - if (replaceNewlines) { - result = result.replace("\n", "").replace("\r", ""); + if (replaceNewlinesAndControlCharacters) { + result = NEWLINE_PATTERN.matcher(result).replaceAll(""); + result = CONTROL_CHARACTER_PATTERN.matcher(result).replaceAll("?"); } if (value instanceof CharSequence) { result = "\"" + result + "\"";