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 from 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.
| Modifier and Type | Field and Description |
|---|---|
| pack-priv static final char | |
| pack-priv static final char | |
| pack-priv static final String | |
| private static final char |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public static final FormattingTuple | |
| public static final FormattingTuple | |
| public static final String | basicArrayFormat(final String messagePattern, final Object[] argArray)
Assumes that argArray only contains arguments with no throwable as last element. |
| public static String | |
| private static void | |
| private static void | |
| private static void | |
| private static void | |
| private static void | |
| private static void | |
| public static final FormattingTuple | |
| public static final FormattingTuple | Returns: The formatted messageThe message pattern which will be parsed and formatted messagePattern, Object The argument to be substituted in place of the first formatting
anchor arg1, Object The argument to be substituted in place of the second formatting
anchor arg2)Performs a two argument substitution for the 'messagePattern' passed as parameter. |
| public static Throwable | getThrowableCandidate(final Object[]
The arguments off which we want to know if it contains a argArray)Throwable as last elementHelper method to determine if an |
| private static void | |
| pack-priv static final boolean | |
| pack-priv static final boolean | |
| private static void | |
| private static void | |
| private static void | |
| private static void | |
| public static Object[] | Returns: a copy of the array without the last elementThe arguments from which we want to remove the last element argArray)Helper method to get all but the last element of an array |
| DELIM_START | back to summary |
|---|---|
| pack-priv static final char DELIM_START | |
| DELIM_STOP | back to summary |
|---|---|
| pack-priv static final char DELIM_STOP | |
| DELIM_STR | back to summary |
|---|---|
| pack-priv static final String DELIM_STR | |
| ESCAPE_CHAR | back to summary |
|---|---|
| private static final char ESCAPE_CHAR | |
| MessageFormatter | back to summary |
|---|---|
| public MessageFormatter() | |
| arrayFormat | back to summary |
|---|---|
| public static final FormattingTuple arrayFormat(final String messagePattern, final Object[] argArray) | |
| arrayFormat | back to summary |
|---|---|
| public static final FormattingTuple arrayFormat(final String messagePattern, final Object[] argArray, Throwable throwable) | |
| basicArrayFormat | back to summary |
|---|---|
| public static final String basicArrayFormat(final String messagePattern, final Object[] argArray) Assumes that argArray only contains arguments with no throwable as last element. | |
| basicArrayFormat | back to summary |
|---|---|
| public static String basicArrayFormat(NormalizedParameters np) | |
| booleanArrayAppend | back to summary |
|---|---|
| private static void booleanArrayAppend(StringBuilder sbuf, boolean[] a) | |
| byteArrayAppend | back to summary |
|---|---|
| private static void byteArrayAppend(StringBuilder sbuf, byte[] a) | |
| charArrayAppend | back to summary |
|---|---|
| private static void charArrayAppend(StringBuilder sbuf, char[] a) | |
| deeplyAppendParameter | back to summary |
|---|---|
| private static void deeplyAppendParameter(StringBuilder sbuf, Object o, Map<Object[], Object> seenMap) | |
| doubleArrayAppend | back to summary |
|---|---|
| private static void doubleArrayAppend(StringBuilder sbuf, double[] a) | |
| floatArrayAppend | back to summary |
|---|---|
| private static void floatArrayAppend(StringBuilder sbuf, float[] a) | |
| format | back to summary |
|---|---|
| public static final FormattingTuple format(String messagePattern, Object arg) Performs single argument substitution for the 'messagePattern' passed as parameter. For example,
MessageFormatter.format("Hi {}.", "there");
will return the string "Hi there.".
| |
| format | back to summary |
|---|---|
| public static final FormattingTuple format(final String messagePattern, Object arg1, Object arg2) 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.".
| |
| getThrowableCandidate | back to summary |
|---|---|
| public static Throwable getThrowableCandidate(final Object[] argArray) Helper method to determine if an | |
| intArrayAppend | back to summary |
|---|---|
| private static void intArrayAppend(StringBuilder sbuf, int[] a) | |
| isDoubleEscaped | back to summary |
|---|---|
| pack-priv static final boolean isDoubleEscaped(String messagePattern, int delimeterStartIndex) | |
| isEscapedDelimeter | back to summary |
|---|---|
| pack-priv static final boolean isEscapedDelimeter(String messagePattern, int delimeterStartIndex) | |
| longArrayAppend | back to summary |
|---|---|
| private static void longArrayAppend(StringBuilder sbuf, long[] a) | |
| objectArrayAppend | back to summary |
|---|---|
| private static void objectArrayAppend(StringBuilder sbuf, Object[] a, Map<Object[], Object> seenMap) | |
| safeObjectAppend | back to summary |
|---|---|
| private static void safeObjectAppend(StringBuilder sbuf, Object o) | |
| shortArrayAppend | back to summary |
|---|---|
| private static void shortArrayAppend(StringBuilder sbuf, short[] a) | |
| trimmedCopy | back to summary |
|---|---|
| public static Object[] trimmedCopy(final Object[] argArray) Helper method to get all but the last element of an array | |