Class LogFormatUtils

java.lang.Object
org.springframework.core.log.LogFormatUtils

public abstract class LogFormatUtils extends Object
Utility methods for formatting and logging messages.

Mainly for internal use within the framework with Apache Commons Logging, typically in the form of the spring-jcl bridge but also compatible with other Commons Logging bridges.

Since:
5.1
Author:
Rossen Stoyanchev, Juergen Hoeller
  • Constructor Details

    • LogFormatUtils

      public LogFormatUtils()
  • Method Details

    • formatValue

      public static String formatValue(@Nullable Object value, boolean limitLength)
      Convenience variant of formatValue(Object, int, boolean) that limits the length of a log message to 100 characters and also replaces newline and control characters if limitLength is set to "true".
      Parameters:
      value - the value to format
      limitLength - whether to truncate the value at a length of 100
      Returns:
      the formatted value
    • formatValue

      public static String formatValue(@Nullable Object value, int maxLength, boolean replaceNewlinesAndControlCharacters)
      Format the given value via toString(), quoting it if it is a CharSequence, truncating at the specified maxLength, and compacting it into a single line when replaceNewLines is set.
      Parameters:
      value - the value to be formatted
      maxLength - the max length, after which to truncate, or -1 for unlimited
      replaceNewlinesAndControlCharacters - whether to replace newline and control characters with placeholders
      Returns:
      the formatted value
    • traceDebug

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