001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.core.log;
018
019import java.util.function.Function;
020
021import org.apache.commons.logging.Log;
022
023import org.springframework.lang.Nullable;
024
025/**
026 * Utility methods for formatting and logging messages.
027 *
028 * <p>Mainly for internal use within the framework with Apache Commons Logging,
029 * typically in the form of the {@code spring-jcl} bridge but also compatible
030 * with other Commons Logging bridges.
031 *
032 * @author Rossen Stoyanchev
033 * @author Juergen Hoeller
034 * @since 5.1
035 */
036public abstract class LogFormatUtils {
037
038        /**
039         * Format the given value via {@code toString()}, quoting it if it is a
040         * {@link CharSequence}, and possibly truncating at 100 if limitLength is
041         * set to true.
042         * @param value the value to format
043         * @param limitLength whether to truncate large formatted values (over 100)
044         * @return the formatted value
045         */
046        public static String formatValue(@Nullable Object value, boolean limitLength) {
047                if (value == null) {
048                        return "";
049                }
050                String str;
051                if (value instanceof CharSequence) {
052                        str = "\"" + value + "\"";
053                }
054                else {
055                        try {
056                                str = value.toString();
057                        }
058                        catch (Throwable ex) {
059                                str = ex.toString();
060                        }
061                }
062                return (limitLength && str.length() > 100 ? str.substring(0, 100) + " (truncated)..." : str);
063        }
064
065        /**
066         * Use this to log a message with different levels of detail (or different
067         * messages) at TRACE vs DEBUG log levels. Effectively, a substitute for:
068         * <pre class="code">
069         * if (logger.isDebugEnabled()) {
070         *   String str = logger.isTraceEnabled() ? "..." : "...";
071         *   if (logger.isTraceEnabled()) {
072         *     logger.trace(str);
073         *   }
074         *   else {
075         *     logger.debug(str);
076         *   }
077         * }
078         * </pre>
079         * @param logger the logger to use to log the message
080         * @param messageFactory function that accepts a boolean set to the value
081         * of {@link Log#isTraceEnabled()}
082         */
083        public static void traceDebug(Log logger, Function<Boolean, String> messageFactory) {
084                if (logger.isDebugEnabled()) {
085                        boolean traceEnabled = logger.isTraceEnabled();
086                        String logMessage = messageFactory.apply(traceEnabled);
087                        if (traceEnabled) {
088                                logger.trace(logMessage);
089                        }
090                        else {
091                                logger.debug(logMessage);
092                        }
093                }
094        }
095
096}