Top Description Fields Constructors Methods
org.springframework.core.log

public abstract Class LogFormatUtils

extends Object
Class Inheritance
Imports
java.util.function.Function, java.util.regex.Pattern, org.apache.commons.logging.Log, org.springframework.lang.Nullable, org.springframework.util.ObjectUtils, .StringUtils

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.

Authors
Rossen Stoyanchev, Juergen Hoeller
Since
5.1

Field Summary

Modifier and TypeField and Description
private static final Pattern
private static final Pattern

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

the formatted value
formatValue
(Object
the value to format
value
,
boolean
whether to truncate the value at a length of 100
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".

public static String

Returns:

the formatted value
formatValue
(Object
the value to be formatted
value
,
int
the max length, after which to truncate, or -1 for unlimited
maxLength
,
boolean
whether to replace newline and control characters with placeholders
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.

public static void
traceDebug(Log
the logger to use to log the message
logger
,
Function<Boolean, String>
function that accepts a boolean set to the value of Log#isTraceEnabled()
messageFactory
)

Use this to log a message with different levels of detail (or different messages) at TRACE vs DEBUG log levels.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CONTROL_CHARACTER_PATTERNback to summary
private static final Pattern CONTROL_CHARACTER_PATTERN
NEWLINE_PATTERNback to summary
private static final Pattern NEWLINE_PATTERN

Constructor Detail

LogFormatUtilsback to summary
public LogFormatUtils()

Method Detail

formatValueback to summary
public static String formatValue(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:Object

the value to format

limitLength:boolean

whether to truncate the value at a length of 100

Returns:String

the formatted value

formatValueback to summary
public static String formatValue(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:Object

the value to be formatted

maxLength:int

the max length, after which to truncate, or -1 for unlimited

replaceNewlinesAndControlCharacters:boolean

whether to replace newline and control characters with placeholders

Returns:String

the formatted value

traceDebugback to summary
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:Log

the logger to use to log the message

messageFactory:Function<Boolean, String>

function that accepts a boolean set to the value of Log#isTraceEnabled()