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 @@ @@ -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; @@ -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 { @@ -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 { @@ -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", "<LF>").replace("\r", "<CR>");
if (replaceNewlinesAndControlCharacters) {
result = NEWLINE_PATTERN.matcher(result).replaceAll("<EOL>");
result = CONTROL_CHARACTER_PATTERN.matcher(result).replaceAll("?");
}
if (value instanceof CharSequence) {
result = "\"" + result + "\"";

Loading…
Cancel
Save