Class MessageFormatter


  • 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.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String DELIM_STR  
      private static char ESCAPE_CHAR  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private MessageFormatter()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static 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.
      private static void booleanArrayAppend​(java.lang.StringBuilder sbuf, boolean[] a)  
      private static void byteArrayAppend​(java.lang.StringBuilder sbuf, byte[] a)  
      private static void charArrayAppend​(java.lang.StringBuilder sbuf, char[] a)  
      private static void deeplyAppendParameter​(java.lang.StringBuilder sbuf, java.lang.Object o, java.util.Set<java.lang.Object[]> seenSet)  
      private static void doubleArrayAppend​(java.lang.StringBuilder sbuf, double[] a)  
      private static void floatArrayAppend​(java.lang.StringBuilder sbuf, float[] a)  
      (package private) static FormattingTuple format​(java.lang.String messagePattern, java.lang.Object arg)
      Performs single argument substitution for the 'messagePattern' passed as parameter.
      (package private) static FormattingTuple format​(java.lang.String messagePattern, java.lang.Object argA, java.lang.Object argB)
      Performs a two argument substitution for the 'messagePattern' passed as parameter.
      private static void intArrayAppend​(java.lang.StringBuilder sbuf, int[] a)  
      private static void longArrayAppend​(java.lang.StringBuilder sbuf, long[] a)  
      private static void objectArrayAppend​(java.lang.StringBuilder sbuf, java.lang.Object[] a, java.util.Set<java.lang.Object[]> seenSet)  
      private static void safeObjectAppend​(java.lang.StringBuilder sbuf, java.lang.Object o)  
      private static void shortArrayAppend​(java.lang.StringBuilder sbuf, short[] a)  
      • Methods inherited from class java.lang.Object

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

      • MessageFormatter

        private MessageFormatter()
    • Method Detail

      • format

        static 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

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

        static 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.
        Parameters:
        messagePattern - The message pattern which will be parsed and formatted
        argArray - An array of arguments to be substituted in place of formatting anchors
        Returns:
        The formatted message
      • deeplyAppendParameter

        private static void deeplyAppendParameter​(java.lang.StringBuilder sbuf,
                                                  java.lang.Object o,
                                                  java.util.Set<java.lang.Object[]> seenSet)
      • safeObjectAppend

        private static void safeObjectAppend​(java.lang.StringBuilder sbuf,
                                             java.lang.Object o)
      • objectArrayAppend

        private static void objectArrayAppend​(java.lang.StringBuilder sbuf,
                                              java.lang.Object[] a,
                                              java.util.Set<java.lang.Object[]> seenSet)
      • booleanArrayAppend

        private static void booleanArrayAppend​(java.lang.StringBuilder sbuf,
                                               boolean[] a)
      • byteArrayAppend

        private static void byteArrayAppend​(java.lang.StringBuilder sbuf,
                                            byte[] a)
      • charArrayAppend

        private static void charArrayAppend​(java.lang.StringBuilder sbuf,
                                            char[] a)
      • shortArrayAppend

        private static void shortArrayAppend​(java.lang.StringBuilder sbuf,
                                             short[] a)
      • intArrayAppend

        private static void intArrayAppend​(java.lang.StringBuilder sbuf,
                                           int[] a)
      • longArrayAppend

        private static void longArrayAppend​(java.lang.StringBuilder sbuf,
                                            long[] a)
      • floatArrayAppend

        private static void floatArrayAppend​(java.lang.StringBuilder sbuf,
                                             float[] a)
      • doubleArrayAppend

        private static void doubleArrayAppend​(java.lang.StringBuilder sbuf,
                                              double[] a)