Class MessageFormatter


  • public final class MessageFormatter
    extends Object
    Slf4J compatible message formatter.

    The MessageFormatter supports formatting a message using Slf4J style message anchors:
     msg("This {} a {} message {}", "is", "simple", "example");
     
    will return "This is a simple message example"

    The MessageFormatter also support named argument binding:
     bind("Hello {:firstName} {:lastName}.",
          arg("firstName", "Peter"),
          arg("lastName", "Hansen"))
     
    will return the string "Hello Peter Hansen."
    • Constructor Detail

      • MessageFormatter

        public MessageFormatter()
    • Method Detail

      • msg

        public static String msg​(String message,
                                 Object... messageAnchorPlaceHolderValues)
        Format a message using Slf4J like message anchors.
        Example: msg("This {} a {} message {}", "is", "simple", "example"); will result in this String:
        "This is a simple message example"
        Parameters:
        message - The message that can contain zero or more message anchors {} that will be replaced positionally by matching values in messageAnchorPlaceHolderValues
        messageAnchorPlaceHolderValues - the positional placeholder values that will be inserted into the message if it contains matching anchors
        Returns:
        the message with any anchors replaced with messageAnchorPlaceHolderValues
      • bind

        public static String bind​(String message,
                                  Map<String,​Object> bindings)
        Replaces placeholders of style "Hello {:firstName} {:lastName}." with bind of firstName to "Peter" and lastName to "Hansen" this will return the string "Hello Peter Hansen.":
             bind("Hello {:firstName} {:lastName}.",
                      Map.of("firstName", "Peter",
                             "lastName", "Hansen"
                      )
                 )
         
        Parameters:
        message - the message string
        bindings - the map where the key will become the MessageFormatter.NamedArgumentBinding.name and the value will become the MessageFormatter.NamedArgumentBinding.value
        Values are converted to String's using the toString()
        Returns:
        the message merged with the bindings