Browse Source

Replace both EOL and control characters

pull/28694/head
Rossen Stoyanchev 4 years ago
parent
commit
50f6db2704
  1. 23
      spring-core/src/main/java/org/springframework/core/log/LogFormatUtils.java

23
spring-core/src/main/java/org/springframework/core/log/LogFormatUtils.java

@ -17,6 +17,7 @@
package org.springframework.core.log; package org.springframework.core.log;
import java.util.function.Function; import java.util.function.Function;
import java.util.regex.Pattern;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -35,9 +36,15 @@ import org.springframework.lang.Nullable;
*/ */
public abstract class LogFormatUtils { 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 * Convenience variant of {@link #formatValue(Object, int, boolean)} that
* method that truncates at 100 characters when {@code limitLength} is set. * 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 value the value to format
* @param limitLength whether to truncate the value at a length of 100 * @param limitLength whether to truncate the value at a length of 100
* @return the formatted value * @return the formatted value
@ -52,10 +59,13 @@ public abstract class LogFormatUtils {
* compacting it into a single line when {@code replaceNewLines} is set. * compacting it into a single line when {@code replaceNewLines} is set.
* @param value the value to be formatted * @param value the value to be formatted
* @param maxLength the max length, after which to truncate, or -1 for unlimited * @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 * @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) { if (value == null) {
return ""; return "";
} }
@ -69,8 +79,9 @@ public abstract class LogFormatUtils {
if (maxLength != -1) { if (maxLength != -1) {
result = (result.length() > maxLength ? result.substring(0, maxLength) + " (truncated)..." : result); result = (result.length() > maxLength ? result.substring(0, maxLength) + " (truncated)..." : result);
} }
if (replaceNewlines) { if (replaceNewlinesAndControlCharacters) {
result = result.replace("\n", "<LF>").replace("\r", "<CR>"); result = NEWLINE_PATTERN.matcher(result).replaceAll("<EOL>");
result = CONTROL_CHARACTER_PATTERN.matcher(result).replaceAll("?");
} }
if (value instanceof CharSequence) { if (value instanceof CharSequence) {
result = "\"" + result + "\""; result = "\"" + result + "\"";

Loading…
Cancel
Save