Merge branch '5.3.x' into main

This commit is contained in:
Rossen Stoyanchev 2021-10-06 21:32:01 +01:00
commit 6eaaf294f3
1 changed files with 28 additions and 10 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -36,30 +36,48 @@ import org.springframework.lang.Nullable;
public abstract class LogFormatUtils { public abstract class LogFormatUtils {
/** /**
* Format the given value via {@code toString()}, quoting it if it is a * Variant of {@link #formatValue(Object, int, boolean)} and a convenience
* {@link CharSequence}, and possibly truncating at 100 if limitLength is * method that truncates at 100 characters when {@code limitLength} is set.
* set to true.
* @param value the value to format * @param value the value to format
* @param limitLength whether to truncate large formatted values (over 100) * @param limitLength whether to truncate the value at a length of 100
* @return the formatted value * @return the formatted value
*/ */
public static String formatValue(@Nullable Object value, boolean limitLength) { public static String formatValue(@Nullable Object value, boolean limitLength) {
return formatValue(value, 100, limitLength);
}
/**
* Format the given value via {@code toString()}, quoting it if it is a
* {@link CharSequence}, truncating at the specified {@code maxLength}, and
* 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
* @return the formatted value
*/
public static String formatValue(@Nullable Object value, int maxLength, boolean replaceNewlines) {
if (value == null) { if (value == null) {
return ""; return "";
} }
String str; String result;
if (value instanceof CharSequence) { if (value instanceof CharSequence) {
str = "\"" + value + "\""; result = "\"" + value + "\"";
} }
else { else {
try { try {
str = value.toString(); result = value.toString();
} }
catch (Throwable ex) { catch (Throwable ex) {
str = ex.toString(); result = ex.toString();
} }
} }
return (limitLength && str.length() > 100 ? str.substring(0, 100) + " (truncated)..." : str); if (maxLength != -1) {
result = (result.length() > maxLength ? result.substring(0, maxLength) + " (truncated)..." : result);
}
if (replaceNewlines) {
result = result.replace("\n", "<LF>").replace("\r", "<CR>");
}
return result;
} }
/** /**