Class ParameterizedMessage
- java.lang.Object
-
- org.apache.logging.log4j.message.ParameterizedMessage
-
- All Implemented Interfaces:
java.io.Serializable,Message,StringBuilderFormattable
public class ParameterizedMessage extends java.lang.Object implements Message, StringBuilderFormattable
AMessageaccepting 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:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classParameterizedMessage.FormatBufferHolder
-
Field Summary
Fields Modifier and Type Field Description private java.lang.Object[]argsstatic java.lang.StringERROR_MSG_SEPARATORSeparator for error messages.static java.lang.StringERROR_PREFIXPrefix for errors.static java.lang.StringERROR_SEPARATORSeparator for errors.static java.lang.StringERROR_SUFFIXSuffix for errors.private static java.lang.ThreadLocal<ParameterizedMessage.FormatBufferHolder>FORMAT_BUFFER_HOLDER_REFprivate java.lang.StringformattedMessageprivate java.lang.Stringpatternprivate ParameterFormatter.MessagePatternAnalysispatternAnalysisstatic java.lang.StringRECURSION_PREFIXPrefix for recursion.static java.lang.StringRECURSION_SUFFIXSuffix for recursion.private static longserialVersionUIDprivate java.lang.Throwablethrowable
-
Constructor Summary
Constructors Constructor Description ParameterizedMessage(java.lang.String pattern, java.lang.Object arg)Constructor with a pattern and a single argument.ParameterizedMessage(java.lang.String pattern, java.lang.Object... args)Constructor with a pattern and multiple arguments.ParameterizedMessage(java.lang.String pattern, java.lang.Object[] args, java.lang.Throwable throwable)Constructs an instance.ParameterizedMessage(java.lang.String pattern, java.lang.Object arg0, java.lang.Object arg1)Constructor with a pattern and two arguments.ParameterizedMessage(java.lang.String pattern, java.lang.String[] args, java.lang.Throwable throwable)Deprecated.Since 2.6, useParameterizedMessage(String, Object[], Throwable)instead
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static intcountArgumentPlaceholders(java.lang.String pattern)Returns the number of argument placeholders.static java.lang.StringdeepToString(java.lang.Object o)This method performs a deep toString of the given Object.private static java.lang.ThrowabledetermineThrowable(java.lang.Throwable throwable, java.lang.Object[] args, ParameterFormatter.MessagePatternAnalysis analysis)booleanequals(java.lang.Object object)static java.lang.Stringformat(java.lang.String pattern, java.lang.Object[] args)Returns the formatted message.voidformatTo(java.lang.StringBuilder buffer)Writes a text representation of this object into the specifiedStringBuilder, ideally without allocating temporary objects.java.lang.StringgetFormat()This method has unclear semantics and inconsistent implementations – its usage is strongly discouraged.java.lang.StringgetFormattedMessage()Returns the formatted message.java.lang.Object[]getParameters()Gets parameter values, if any.java.lang.ThrowablegetThrowable()TheThrowableprovided 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 patterninthashCode()static java.lang.StringidentityToString(java.lang.Object obj)This method returns the same as if Object.toString() would not have been overridden in obj.private voidreadObject(java.io.ObjectInputStream in)java.lang.StringtoString()private voidwriteObject(java.io.ObjectOutputStream out)
-
-
-
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
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
FORMAT_BUFFER_HOLDER_REF
private static final java.lang.ThreadLocal<ParameterizedMessage.FormatBufferHolder> FORMAT_BUFFER_HOLDER_REF
-
pattern
private final java.lang.String pattern
-
args
private transient java.lang.Object[] args
-
throwable
private final transient java.lang.Throwable throwable
-
patternAnalysis
private final ParameterFormatter.MessagePatternAnalysis patternAnalysis
-
formattedMessage
private java.lang.String formattedMessage
-
-
Constructor Detail
-
ParameterizedMessage
@InlineMe(replacement="this(pattern, Arrays.stream(args).toArray(Object[]::new), throwable)", imports="java.util.Arrays") @Deprecated public ParameterizedMessage(java.lang.String pattern, java.lang.String[] args, java.lang.Throwable throwable)Deprecated.Since 2.6, useParameterizedMessage(String, Object[], Throwable)insteadConstructs an instance.The
Throwableassociated with the message (and returned ingetThrowable()) will be determined as follows:- If a
throwableargument is provided - If the last argument is a
Throwableand 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(java.lang.String pattern, java.lang.Object[] args, java.lang.Throwable throwable)Constructs an instance.The
Throwableassociated with the message (and returned ingetThrowable()) will be determined as follows:- If a
throwableargument is provided - If the last argument is a
Throwableand 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(java.lang.String pattern, java.lang.Object... args)Constructor with a pattern and multiple arguments.If the last argument is a
Throwableand 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(java.lang.String pattern, java.lang.Object arg)Constructor with a pattern and a single argument.If the argument is a
Throwableand is not referred to by any placeholder in the pattern, it is returned ingetThrowable().- Parameters:
pattern- a formatting patternarg- an argument
-
ParameterizedMessage
public ParameterizedMessage(java.lang.String pattern, java.lang.Object arg0, java.lang.Object arg1)Constructor with a pattern and two arguments.If the last argument is a
Throwableand 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
-
determineThrowable
private static java.lang.Throwable determineThrowable(java.lang.Throwable throwable, java.lang.Object[] args, ParameterFormatter.MessagePatternAnalysis analysis)
-
getFormat
public java.lang.String getFormat()
Description copied from interface:MessageThis method has unclear semantics and inconsistent implementations – its usage is strongly discouraged.
-
getParameters
public java.lang.Object[] getParameters()
Description copied from interface:MessageGets parameter values, if any.- Specified by:
getParametersin interfaceMessage- Returns:
- the message arguments
-
getThrowable
public java.lang.Throwable getThrowable()
TheThrowableprovided 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:
getThrowablein interfaceMessage- Returns:
- the
Throwableprovided along with the message
-
getFormattedMessage
public java.lang.String getFormattedMessage()
Returns the formatted message.If possible, the result will be cached for subsequent invocations.
- Specified by:
getFormattedMessagein interfaceMessage- Returns:
- the formatted message
-
formatTo
public void formatTo(java.lang.StringBuilder buffer)
Description copied from interface:StringBuilderFormattableWrites a text representation of this object into the specifiedStringBuilder, ideally without allocating temporary objects.- Specified by:
formatToin interfaceStringBuilderFormattable- Parameters:
buffer- the StringBuilder to write into
-
format
public static java.lang.String format(java.lang.String pattern, java.lang.Object[] args)Returns the formatted message.- Parameters:
pattern- a message pattern containing argument placeholdersargs- arguments to be used to replace placeholders
-
equals
public boolean equals(java.lang.Object object)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
countArgumentPlaceholders
public static int countArgumentPlaceholders(java.lang.String pattern)
Returns the number of argument placeholders.- Parameters:
pattern- the message pattern to be analyzed
-
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
Objectdoes 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:
toStringin classjava.lang.Object
-
writeObject
private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException- Throws:
java.io.IOException
-
readObject
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException- Throws:
java.io.IOExceptionjava.lang.ClassNotFoundException
-
-