Class MessageFormatter


  • public final class MessageFormatter
    extends java.lang.Object
    Formats messages according to very simple substitution rules. Substitutions can be made 1, 2 or more arguments.

    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.

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static FormattingTuple arrayFormat​(java.lang.String messagePattern, java.lang.Object[] argArray)  
      static FormattingTuple arrayFormat​(java.lang.String messagePattern, java.lang.Object[] argArray, java.lang.Throwable throwable)  
      static FormattingTuple format​(java.lang.String messagePattern, java.lang.Object arg)
      Performs single argument substitution for the 'messagePattern' passed as parameter.
      static 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.
      static java.lang.Throwable getThrowableCandidate​(java.lang.Object[] argArray)
      Helper method to determine if an Object array contains a Throwable as last element
      static java.lang.Object[] trimmedCopy​(java.lang.Object[] argArray)
      Helper method to get all but the last element of an array
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MessageFormatter

        public MessageFormatter()
    • Method Detail

      • format

        public static final FormattingTuple format​(java.lang.String messagePattern,
                                                   java.lang.Object arg)
        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 formatted
        arg - The argument to be substituted in place of the formatting anchor
        Returns:
        The formatted message
      • format

        public static final 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.

        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 formatted
        arg1 - The argument to be substituted in place of the first formatting anchor
        arg2 - The argument to be substituted in place of the second formatting anchor
        Returns:
        The formatted message
      • arrayFormat

        public static final FormattingTuple arrayFormat​(java.lang.String messagePattern,
                                                        java.lang.Object[] argArray)
      • arrayFormat

        public static final FormattingTuple arrayFormat​(java.lang.String messagePattern,
                                                        java.lang.Object[] argArray,
                                                        java.lang.Throwable throwable)
      • getThrowableCandidate

        public static java.lang.Throwable getThrowableCandidate​(java.lang.Object[] argArray)
        Helper method to determine if an Object array contains a Throwable as last element
        Parameters:
        argArray - The arguments off which we want to know if it contains a Throwable as last element
        Returns:
        if the last Object in argArray is a Throwable this method will return it, otherwise it returns null
      • trimmedCopy

        public static java.lang.Object[] trimmedCopy​(java.lang.Object[] argArray)
        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