Class ParameterizedMessage
- java.lang.Object
-
- org.apache.logging.log4j.message.ParameterizedMessage
-
- All Implemented Interfaces:
java.io.Serializable
,Message
,org.apache.logging.log4j.util.StringBuilderFormattable
public class ParameterizedMessage extends java.lang.Object implements Message, org.apache.logging.log4j.util.StringBuilderFormattable
Handles messages that consist of a format string containing '{}' to represent each replaceable token, and the parameters.This class was originally written for Lilith by Joern Huxhorn where it is licensed under the LGPL. It has been relicensed here with his permission providing that this attribution remain.
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
ERROR_MSG_SEPARATOR
Separator for error messages.static java.lang.String
ERROR_PREFIX
Prefix for errors.static java.lang.String
ERROR_SEPARATOR
Separator for errors.static java.lang.String
ERROR_SUFFIX
Suffix for errors.static java.lang.String
RECURSION_PREFIX
Prefix for recursion.static java.lang.String
RECURSION_SUFFIX
Suffix for recursion.
-
Constructor Summary
Constructors Constructor Description ParameterizedMessage(java.lang.String messagePattern, java.lang.Object arg)
Constructor with a pattern and a single parameter.ParameterizedMessage(java.lang.String messagePattern, java.lang.Object... arguments)
Constructs a ParameterizedMessage which contains the arguments converted to String as well as an optional Throwable.ParameterizedMessage(java.lang.String messagePattern, java.lang.Object[] arguments, java.lang.Throwable throwable)
Creates a parameterized message.ParameterizedMessage(java.lang.String messagePattern, java.lang.Object arg0, java.lang.Object arg1)
Constructor with a pattern and two parameters.ParameterizedMessage(java.lang.String messagePattern, java.lang.String[] arguments, java.lang.Throwable throwable)
Deprecated.Use constructor ParameterizedMessage(String, Object[], Throwable) instead
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static int
countArgumentPlaceholders(java.lang.String messagePattern)
Counts the number of unescaped placeholders in the given messagePattern.static java.lang.String
deepToString(java.lang.Object o)
This method performs a deep toString of the given Object.boolean
equals(java.lang.Object o)
static java.lang.String
format(java.lang.String messagePattern, java.lang.Object[] arguments)
Replace placeholders in the given messagePattern with arguments.void
formatTo(java.lang.StringBuilder buffer)
java.lang.String
getFormat()
Returns the message pattern.java.lang.String
getFormattedMessage()
Returns the formatted message.java.lang.Object[]
getParameters()
Returns the message parameters.java.lang.Throwable
getThrowable()
Returns the Throwable that was given as the last argument, if any.int
hashCode()
static java.lang.String
identityToString(java.lang.Object obj)
This method returns the same as if Object.toString() would not have been overridden in obj.java.lang.String
toString()
-
-
-
Field Detail
-
RECURSION_PREFIX
public static final java.lang.String RECURSION_PREFIX
Prefix for recursion.- See Also:
- Constant Field Values
-
RECURSION_SUFFIX
public static final java.lang.String RECURSION_SUFFIX
Suffix for recursion.- See Also:
- Constant Field Values
-
ERROR_PREFIX
public static final java.lang.String ERROR_PREFIX
Prefix for errors.- See Also:
- Constant Field Values
-
ERROR_SEPARATOR
public static final java.lang.String ERROR_SEPARATOR
Separator for errors.- See Also:
- Constant Field Values
-
ERROR_MSG_SEPARATOR
public static final java.lang.String ERROR_MSG_SEPARATOR
Separator for error messages.- See Also:
- Constant Field Values
-
ERROR_SUFFIX
public static final java.lang.String ERROR_SUFFIX
Suffix for errors.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ParameterizedMessage
@Deprecated public ParameterizedMessage(java.lang.String messagePattern, java.lang.String[] arguments, java.lang.Throwable throwable)
Deprecated.Use constructor ParameterizedMessage(String, Object[], Throwable) insteadCreates a parameterized message.- Parameters:
messagePattern
- The message "format" string. This will be a String containing "{}" placeholders where parameters should be substituted.arguments
- The arguments for substitution.throwable
- A Throwable.
-
ParameterizedMessage
public ParameterizedMessage(java.lang.String messagePattern, java.lang.Object[] arguments, java.lang.Throwable throwable)
Creates a parameterized message.- Parameters:
messagePattern
- The message "format" string. This will be a String containing "{}" placeholders where parameters should be substituted.arguments
- The arguments for substitution.throwable
- A Throwable.
-
ParameterizedMessage
public ParameterizedMessage(java.lang.String messagePattern, java.lang.Object... arguments)
Constructs a ParameterizedMessage which contains the arguments converted to String as well as an optional Throwable.If the last argument is a Throwable and is NOT used up by a placeholder in the message pattern it is returned in
getThrowable()
and won't be contained in the created String[]. If it is used upgetThrowable()
will return null even if the last argument was a Throwable!- Parameters:
messagePattern
- the message pattern that to be checked for placeholders.arguments
- the argument array to be converted.
-
ParameterizedMessage
public ParameterizedMessage(java.lang.String messagePattern, java.lang.Object arg)
Constructor with a pattern and a single parameter.- Parameters:
messagePattern
- The message pattern.arg
- The parameter.
-
ParameterizedMessage
public ParameterizedMessage(java.lang.String messagePattern, java.lang.Object arg0, java.lang.Object arg1)
Constructor with a pattern and two parameters.- Parameters:
messagePattern
- The message pattern.arg0
- The first parameter.arg1
- The second parameter.
-
-
Method Detail
-
getFormat
public java.lang.String getFormat()
Returns the message pattern.
-
getParameters
public java.lang.Object[] getParameters()
Returns the message parameters.- Specified by:
getParameters
in interfaceMessage
- Returns:
- the message parameters.
-
getThrowable
public java.lang.Throwable getThrowable()
Returns the Throwable that was given as the last argument, if any. It will not survive serialization. The Throwable exists as part of the message primarily so that it can be extracted from the end of the list of parameters and then be added to the LogEvent. As such, the Throwable in the event should not be used once the LogEvent has been constructed.- Specified by:
getThrowable
in interfaceMessage
- Returns:
- the Throwable, if any.
-
getFormattedMessage
public java.lang.String getFormattedMessage()
Returns the formatted message.- Specified by:
getFormattedMessage
in interfaceMessage
- Returns:
- the formatted message.
-
formatTo
public void formatTo(java.lang.StringBuilder buffer)
- Specified by:
formatTo
in interfaceorg.apache.logging.log4j.util.StringBuilderFormattable
-
format
public static java.lang.String format(java.lang.String messagePattern, java.lang.Object[] arguments)
Replace placeholders in the given messagePattern with arguments.- Parameters:
messagePattern
- the message pattern containing placeholders.arguments
- the arguments to be used to replace placeholders.- Returns:
- the formatted message.
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
countArgumentPlaceholders
public static int countArgumentPlaceholders(java.lang.String messagePattern)
Counts the number of unescaped placeholders in the given messagePattern.- Parameters:
messagePattern
- the message pattern to be analyzed.- Returns:
- the number of unescaped placeholders.
-
deepToString
public static java.lang.String deepToString(java.lang.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 java.lang.String identityToString(java.lang.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 java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-