/*
 * Copyright 2002-2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.core.log;

import java.util.function.Function;

import org.apache.commons.logging.Log;

import org.springframework.lang.Nullable;

/**
 * Utility methods for formatting and logging messages.
 *
 * <p>Mainly for internal use within the framework with Apache Commons Logging,
 * typically in the form of the {@code spring-jcl} bridge but also compatible
 * with other Commons Logging bridges.
 *
 * @author Rossen Stoyanchev
 * @author Juergen Hoeller
 * @since 5.1
 */
public abstract class LogFormatUtils {

	/**
	 * Convenience variant of {@link #formatValue(Object, int, boolean)} that
	 * limits the length of a log message to 100 characters and also replaces
	 * newline 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
	 */
	public static String formatValue(@Nullable Object value, boolean limitLength) {
		return formatValue(value, (limitLength ? 100 : -1), 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) {
			return "";
		}
		String result;
		try {
			result = value.toString();
		}
		catch (Throwable ex) {
			result = ex.toString();
		}
		if (maxLength != -1) {
			result = (result.length() > maxLength ? result.substring(0, maxLength) + " (truncated)..." : result);
		}
		if (replaceNewlines) {
			result = result.replace("\n", "<LF>").replace("\r", "<CR>");
		}
		if (value instanceof CharSequence) {
			result = "\"" + result + "\"";
		}
		return result;
	}

	/**
	 * Use this to log a message with different levels of detail (or different
	 * messages) at TRACE vs DEBUG log levels. Effectively, a substitute for:
	 * <pre class="code">
	 * if (logger.isDebugEnabled()) {
	 *   String str = logger.isTraceEnabled() ? "..." : "...";
	 *   if (logger.isTraceEnabled()) {
	 *     logger.trace(str);
	 *   }
	 *   else {
	 *     logger.debug(str);
	 *   }
	 * }
	 * </pre>
	 * @param logger the logger to use to log the message
	 * @param messageFactory function that accepts a boolean set to the value
	 * of {@link Log#isTraceEnabled()}
	 */
	public static void traceDebug(Log logger, Function<Boolean, String> messageFactory) {
		if (logger.isDebugEnabled()) {
			boolean traceEnabled = logger.isTraceEnabled();
			String logMessage = messageFactory.apply(traceEnabled);
			if (traceEnabled) {
				logger.trace(logMessage);
			}
			else {
				logger.debug(logMessage);
			}
		}
	}

}

@@ -690,16 +690,48 @@
 unction;
+%0Aimport java.util.regex.Pattern;
 %0A%0Aimport
@@ -1184,16 +1184,180 @@
 tils %7B%0A%0A
+%09private static final Pattern NEWLINE_PATTERN = Pattern.compile(%22%5B%5Cn%5Cr%5D%22);%0A%0A%09private static final Pattern CONTROL_CHARACTER_PATTERN = Pattern.compile(%22%5C%5Cp%7BCc%7D%22);%0A%0A%0A
 %09/**%0A%09 *
@@ -1507,32 +1507,44 @@
 ces%0A%09 * newline 
+and control 
 characters if %7B@
@@ -2258,16 +2258,36 @@
 Newlines
+AndControlCharacters
  whether
@@ -2306,16 +2306,32 @@
 newline 
+and%0A%09 * control 
 characte
@@ -2414,32 +2414,36 @@
 ing formatValue(
+%0A%09%09%09
 @Nullable Object
@@ -2488,20 +2488,41 @@
 Newlines
+AndControlCharacters
 ) %7B%0A
+%0A
 %09%09if (va
@@ -2828,38 +2828,83 @@
 ines
-) %7B%0A%09%09%09result = 
+AndControlCharacters) %7B%0A%09%09%09result = NEWLINE_PATTERN.matcher(
 result
+)
 .replace
 (%22%5Cn
@@ -2903,42 +2903,89 @@
 lace
-(%22%5Cn%22, %22%3CLF%3E%22).replace(%22%5Cr%22, %22%3CCR%3E
+All(%22%3CEOL%3E%22);%0A%09%09%09result = CONTROL_CHARACTER_PATTERN.matcher(result).replaceAll(%22?
 %22);%0A

Replace both EOL and control characters