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
AMessage
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}
This class was originally written for Lilith by Jörn Huxhorn and 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 String
ERROR_MSG_SEPARATOR
Separator for error messages.static String
ERROR_PREFIX
Prefix for errors.static String
ERROR_SEPARATOR
Separator for errors.static String
ERROR_SUFFIX
Suffix for errors.static String
RECURSION_PREFIX
Prefix for recursion.static String
RECURSION_SUFFIX
Suffix for recursion.
-
Constructor Summary
Constructors Constructor Description ParameterizedMessage(String pattern, Object arg)
Constructor with a pattern and a single argument.ParameterizedMessage(String pattern, Object... args)
Constructor with a pattern and multiple arguments.ParameterizedMessage(String pattern, Object[] args, Throwable throwable)
Constructs an instance.ParameterizedMessage(String pattern, Object arg0, Object arg1)
Constructor with a pattern and two arguments.ParameterizedMessage(String pattern, String[] args, Throwable throwable)
Deprecated.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static int
countArgumentPlaceholders(String pattern)
static String
deepToString(Object o)
This method performs a deep toString of the given Object.boolean
equals(Object object)
static String
format(String pattern, Object[] args)
void
formatTo(StringBuilder buffer)
Writes a text representation of this object into the specifiedStringBuilder
, ideally without allocating temporary objects.String
getFormat()
Gets the format portion of the Message.String
getFormattedMessage()
Returns the formatted message.Object[]
getParameters()
Gets parameter values, if any.Throwable
getThrowable()
TheThrowable
provided along with the message by one of the following means: explicitly in the constructor as the last message argument that is not referred to by any placeholder in the formatting patternint
hashCode()
static String
identityToString(Object obj)
This method returns the same as if Object.toString() would not have been overridden in obj.String
toString()
-
-
-
Field Detail
-
RECURSION_PREFIX
public static final String RECURSION_PREFIX
Prefix for recursion.- See Also:
- Constant Field Values
-
RECURSION_SUFFIX
public static final String RECURSION_SUFFIX
Suffix for recursion.- See Also:
- Constant Field Values
-
ERROR_PREFIX
public static final String ERROR_PREFIX
Prefix for errors.- See Also:
- Constant Field Values
-
ERROR_SEPARATOR
public static final String ERROR_SEPARATOR
Separator for errors.- See Also:
- Constant Field Values
-
ERROR_MSG_SEPARATOR
public static final String ERROR_MSG_SEPARATOR
Separator for error messages.- See Also:
- Constant Field Values
-
ERROR_SUFFIX
public static final String ERROR_SUFFIX
Suffix for errors.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ParameterizedMessage
@Deprecated public ParameterizedMessage(String pattern, String[] args, Throwable throwable)
Deprecated.Constructs an instance.The
Throwable
associated with the message (and returned ingetThrowable()
) will be determined as follows:- If a
throwable
argument is provided - If the last argument is a
Throwable
and is not referred to by any placeholder in the pattern
- Parameters:
pattern
- a formatting patternargs
- arguments to be formattedthrowable
- aThrowable
- If a
-
ParameterizedMessage
public ParameterizedMessage(String pattern, Object[] args, Throwable throwable)
Constructs an instance.The
Throwable
associated with the message (and returned ingetThrowable()
) will be determined as follows:- If a
throwable
argument is provided - If the last argument is a
Throwable
and is not referred to by any placeholder in the pattern
- Parameters:
pattern
- a formatting patternargs
- arguments to be formattedthrowable
- aThrowable
- If a
-
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 ingetThrowable()
.- Parameters:
pattern
- a formatting patternargs
- 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 ingetThrowable()
.- Parameters:
pattern
- a formatting patternarg
- 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 ingetThrowable()
and won't be contained in the formatted message.- Parameters:
pattern
- a formatting patternarg0
- the first argumentarg1
- the second argument
-
-
Method Detail
-
getFormat
public String getFormat()
Description copied from interface:Message
Gets the format portion of the Message.
-
getParameters
public Object[] getParameters()
Description copied from interface:Message
Gets parameter values, if any.- Specified by:
getParameters
in interfaceMessage
- Returns:
- the message arguments
-
getThrowable
public Throwable getThrowable()
TheThrowable
provided along with the message by one of the following means:- explicitly in the constructor
- as the last message argument that is not referred to by any placeholder in the formatting pattern
- Specified by:
getThrowable
in interfaceMessage
- 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 interfaceMessage
- Returns:
- the formatted message
-
formatTo
public void formatTo(StringBuilder buffer)
Description copied from interface:StringBuilderFormattable
Writes a text representation of this object into the specifiedStringBuilder
, ideally without allocating temporary objects.- Specified by:
formatTo
in interfaceStringBuilderFormattable
- Parameters:
buffer
- the StringBuilder to write into
-
format
public static String format(String pattern, Object[] args)
- Parameters:
pattern
- a message pattern containing argument placeholdersargs
- arguments to be used to replace placeholders- Returns:
- the formatted message
-
countArgumentPlaceholders
public static int countArgumentPlaceholders(String pattern)
- Parameters:
pattern
- the message pattern to be analyzed- Returns:
- the number of argument placeholders
-
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()
-
-