Class MessageFormatter
For example,
MessageFormatter.format("Hi {}.", "there")will return the string "Hi there.".
The {} pair is called the formatting anchor. It serves to designate the location where arguments need to be substituted within the message pattern.
In case your message contains the '{' or the '}' character, you do not have to do anything special unless the '}' character immediately follows '{'. For example,
MessageFormatter.format("Set {1,2,3} is not equal to {}.", "1,2");will return the string "Set {1,2,3} is not equal to 1,2.".
If for whatever reason you need to place the string "{}" in the message without its formatting anchor meaning, then you need to escape the '{' character with '\', that is the backslash character. Only the '{' character should be escaped. There is no need to escape the '}' character. For example,
MessageFormatter.format("Set \\{} is not equal to {}.", "1,2");will return the string "Set {} is not equal to 1,2.".
The escaping behavior just described can be overridden by escaping the escape character '\'. Calling
MessageFormatter.format("File name is C:\\\\{}.", "file.zip");will return the string "File name is C:\file.zip".
The formatting conventions are different than those of MessageFormat
which ships with the Java platform. This is justified by the fact that
SLF4J's implementation is 10 times faster than that of MessageFormat
.
This local performance difference is both measurable and significant in the
larger context of the complete logging processing chain.
See also format(String, Object)
,
format(String, Object, Object)
and
arrayFormat(String, Object[])
methods for more details.
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final char
(package private) static final char
(package private) static final String
private static final char
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic final FormattingTuple
arrayFormat
(String messagePattern, Object[] argArray) static final FormattingTuple
arrayFormat
(String messagePattern, Object[] argArray, Throwable throwable) private static void
booleanArrayAppend
(StringBuilder sbuf, boolean[] a) private static void
byteArrayAppend
(StringBuilder sbuf, byte[] a) private static void
charArrayAppend
(StringBuilder sbuf, char[] a) private static void
deeplyAppendParameter
(StringBuilder sbuf, Object o, Map<Object[], Object> seenMap) private static void
doubleArrayAppend
(StringBuilder sbuf, double[] a) private static void
floatArrayAppend
(StringBuilder sbuf, float[] a) static final FormattingTuple
Performs single argument substitution for the 'messagePattern' passed as parameter.static final FormattingTuple
Performs a two argument substitution for the 'messagePattern' passed as parameter.static Throwable
getThrowableCandidate
(Object[] argArray) private static void
intArrayAppend
(StringBuilder sbuf, int[] a) (package private) static final boolean
isDoubleEscaped
(String messagePattern, int delimeterStartIndex) (package private) static final boolean
isEscapedDelimeter
(String messagePattern, int delimeterStartIndex) private static void
longArrayAppend
(StringBuilder sbuf, long[] a) private static void
objectArrayAppend
(StringBuilder sbuf, Object[] a, Map<Object[], Object> seenMap) private static void
safeObjectAppend
(StringBuilder sbuf, Object o) private static void
shortArrayAppend
(StringBuilder sbuf, short[] a) static Object[]
trimmedCopy
(Object[] argArray) Helper method to get all but the last element of an array
-
Field Details
-
DELIM_START
static final char DELIM_START- See Also:
-
DELIM_STOP
static final char DELIM_STOP- See Also:
-
DELIM_STR
- See Also:
-
ESCAPE_CHAR
private static final char ESCAPE_CHAR- See Also:
-
-
Constructor Details
-
MessageFormatter
public MessageFormatter()
-
-
Method Details
-
format
Performs single argument substitution for the 'messagePattern' passed as parameter.For example,
MessageFormatter.format("Hi {}.", "there");
will return the string "Hi there.".- Parameters:
messagePattern
- The message pattern which will be parsed and formattedarg
- The argument to be substituted in place of the formatting anchor- Returns:
- The formatted message
-
format
Performs a two argument substitution for the 'messagePattern' passed as parameter.For example,
MessageFormatter.format("Hi {}. My name is {}.", "Alice", "Bob");
will return the string "Hi Alice. My name is Bob.".- Parameters:
messagePattern
- The message pattern which will be parsed and formattedarg1
- The argument to be substituted in place of the first formatting anchorarg2
- The argument to be substituted in place of the second formatting anchor- Returns:
- The formatted message
-
arrayFormat
-
arrayFormat
public static final FormattingTuple arrayFormat(String messagePattern, Object[] argArray, Throwable throwable) -
isEscapedDelimeter
-
isDoubleEscaped
-
deeplyAppendParameter
private static void deeplyAppendParameter(StringBuilder sbuf, Object o, Map<Object[], Object> seenMap) -
safeObjectAppend
-
objectArrayAppend
-
booleanArrayAppend
-
byteArrayAppend
-
charArrayAppend
-
shortArrayAppend
-
intArrayAppend
-
longArrayAppend
-
floatArrayAppend
-
doubleArrayAppend
-
getThrowableCandidate
-
trimmedCopy
Helper method to get all but the last element of an array- Parameters:
argArray
- The arguments from which we want to remove the last element- Returns:
- a copy of the array without the last element
-