public final class MessageFormatter
extends java.lang.Object
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
java.text.MessageFormat
which ships with the Java
platform. This is justified by the fact that SLF4J's implementation is 10 times faster than that of java.text.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.
This class was adapted for JSimple from SLF4J, version 1.7.5 The primary change was removing support for including
array contents in formatted messages with array args. That was primarily made to reduce code size and make things a
bit simpler, keeping with the JSimple philosophy. Callers that wish to have fancy formatting for any logged messages
they have with array arguments can create a wrapper class with a toString method that formats as they desire. In
many cases that's more useful anyway as it gives the caller control in how the array data should be formatted, so
it's most user readable and not excessively big (e.g. maybe they only want to log first X elements).Modifier and Type | Class and Description |
---|---|
static class |
MessageFormatter.FormattingTuple
Holds the results of formatting done by
MessageFormatter . |
Constructor and Description |
---|
MessageFormatter() |
Modifier and Type | Method and Description |
---|---|
static MessageFormatter.FormattingTuple |
arrayFormat(java.lang.String messagePattern,
java.lang.Object[] argArray)
Same principle as the
format(String, Object) and format(String, Object, Object) methods except
that any number of arguments can be passed in an array. |
static MessageFormatter.FormattingTuple |
format(java.lang.String messagePattern,
java.lang.Object arg)
Performs single argument substitution for the 'messagePattern' passed as parameter.
|
static MessageFormatter.FormattingTuple |
format(java.lang.String messagePattern,
java.lang.Object arg1,
java.lang.Object arg2)
Performs a two argument substitution for the 'messagePattern' passed as parameter.
|
public static MessageFormatter.FormattingTuple format(java.lang.String messagePattern, java.lang.Object arg)
MessageFormatter.format("Hi {}.", "there");will return the string "Hi there.".
messagePattern
- The message pattern which will be parsed and formattedarg
- The argument to be substituted in place of the formatting anchorpublic static MessageFormatter.FormattingTuple format(java.lang.String messagePattern, java.lang.Object arg1, java.lang.Object arg2)
MessageFormatter.format("Hi {}. My name is {}.", "Alice", "Bob");will return the string "Hi Alice. My name is Bob.".
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 anchorpublic static MessageFormatter.FormattingTuple arrayFormat(java.lang.String messagePattern, @Nullable java.lang.Object[] argArray)
format(String, Object)
and format(String, Object, Object)
methods except
that any number of arguments can be passed in an array.messagePattern
- The message pattern which will be parsed and formattedargArray
- An array of arguments to be substituted in place of formatting anchors