Class ParameterizedMessage

java.lang.Object
org.apache.logging.log4j.message.ParameterizedMessage
All Implemented Interfaces:
Serializable, Message, StringBuilderFormattable

public class ParameterizedMessage extends Object implements Message, StringBuilderFormattable
A Message accepting argument placeholders in the formatting pattern.

Only "{}" strings are treated as argument placeholders. Escaped (i.e., "\"-prefixed) or incomplete argument placeholders will be ignored. Examples of argument placeholders that will be discarded and rendered intact:

 { }
 foo\{}
 {bar
 {buzz}
 
See Also:
  • Field Details

  • Constructor Details

    • ParameterizedMessage

      @InlineMe(replacement="this(pattern, Arrays.stream(args).toArray(Object[]::new), throwable)", imports="java.util.Arrays") @Deprecated public ParameterizedMessage(String pattern, String[] args, Throwable throwable)
      Constructs an instance.

      The Throwable associated with the message (and returned in getThrowable()) will be determined as follows:

      1. If a throwable argument is provided
      2. If the last argument is a Throwable and is not referred to by any placeholder in the pattern
      Parameters:
      pattern - a formatting pattern
      args - arguments to be formatted
      throwable - a Throwable
    • ParameterizedMessage

      public ParameterizedMessage(String pattern, Object[] args, Throwable throwable)
      Constructs an instance.

      The Throwable associated with the message (and returned in getThrowable()) will be determined as follows:

      1. If a throwable argument is provided
      2. If the last argument is a Throwable and is not referred to by any placeholder in the pattern
      Parameters:
      pattern - a formatting pattern
      args - arguments to be formatted
      throwable - a Throwable
    • ParameterizedMessage

      public ParameterizedMessage(String pattern, Object... args)
      Constructor with a pattern and multiple arguments.

      If the last argument is a Throwable and is not referred to by any placeholder in the pattern, it is returned in getThrowable().

      Parameters:
      pattern - a formatting pattern
      args - arguments to be formatted
    • ParameterizedMessage

      public ParameterizedMessage(String pattern, Object arg)
      Constructor with a pattern and a single argument.

      If the argument is a Throwable and is not referred to by any placeholder in the pattern, it is returned in getThrowable().

      Parameters:
      pattern - a formatting pattern
      arg - an argument
    • ParameterizedMessage

      public ParameterizedMessage(String pattern, Object arg0, Object arg1)
      Constructor with a pattern and two arguments.

      If the last argument is a Throwable and is not referred to by any placeholder in the pattern, it is returned in getThrowable() and won't be contained in the formatted message.

      Parameters:
      pattern - a formatting pattern
      arg0 - the first argument
      arg1 - the second argument
  • Method Details

    • getFormat

      public String getFormat()
      Description copied from interface: Message
      Gets the format portion of the Message.
      Specified by:
      getFormat in interface Message
      Returns:
      the message formatting pattern
    • getParameters

      public Object[] getParameters()
      Description copied from interface: Message
      Gets parameter values, if any.
      Specified by:
      getParameters in interface Message
      Returns:
      the message arguments
    • getThrowable

      public Throwable getThrowable()
      The Throwable provided along with the message by one of the following means:
      1. explicitly in the constructor
      2. as the last message argument that is not referred to by any placeholder in the formatting pattern
      Specified by:
      getThrowable in interface Message
      Returns:
      the Throwable provided along with the message
    • getFormattedMessage

      public String getFormattedMessage()
      Returns the formatted message.

      If possible, the result will be cached for subsequent invocations.

      Specified by:
      getFormattedMessage in interface Message
      Returns:
      the formatted message
    • formatTo

      public void formatTo(StringBuilder buffer)
      Description copied from interface: StringBuilderFormattable
      Writes a text representation of this object into the specified StringBuilder, ideally without allocating temporary objects.
      Specified by:
      formatTo in interface StringBuilderFormattable
      Parameters:
      buffer - the StringBuilder to write into
    • format

      public static String format(String pattern, Object[] args)
      Returns the formatted message.
      Parameters:
      pattern - a message pattern containing argument placeholders
      args - arguments to be used to replace placeholders
    • equals

      public boolean equals(Object object)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • countArgumentPlaceholders

      public static int countArgumentPlaceholders(String pattern)
      Returns the number of argument placeholders.
      Parameters:
      pattern - the message pattern to be analyzed
    • deepToString

      public static String deepToString(Object o)
      This method performs a deep toString of the given Object. Primitive arrays are converted using their respective Arrays.toString methods while special handling is implemented for "container types", i.e. Object[], Map and Collection because those could contain themselves.

      It should be noted that neither AbstractMap.toString() nor AbstractCollection.toString() implement such a behavior. They only check if the container is directly contained in itself, but not if a contained container contains the original one. Because of that, Arrays.toString(Object[]) isn't safe either. Confusing? Just read the last paragraph again and check the respective toString() implementation.

      This means, in effect, that logging would produce a usable output even if an ordinary System.out.println(o) would produce a relatively hard-to-debug StackOverflowError.

      Parameters:
      o - The object.
      Returns:
      The String representation.
    • identityToString

      public static String identityToString(Object obj)
      This method returns the same as if Object.toString() would not have been overridden in obj.

      Note that this isn't 100% secure as collisions can always happen with hash codes.

      Copied from Object.hashCode():

      As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.)
      Parameters:
      obj - the Object that is to be converted into an identity string.
      Returns:
      the identity string as also defined in Object.toString()
    • toString

      public String toString()
      Overrides:
      toString in class Object