Top Description Fields Constructors Methods
org.apache.logging.log4j.message

public Class ParameterizedMessage

extends Object
implements Message, StringBuilderFormattable
Class Inheritance
All Implemented Interfaces
org.apache.logging.log4j.util.StringBuilderFormattable, org.apache.logging.log4j.message.Message, java.io.Serializable
Static Imports
org.apache.logging.log4j.message.ParameterFormatter.analyzePattern

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}

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.

Field Summary

Modifier and TypeField and Description
private transient Object[]
public static final String
ERROR_MSG_SEPARATOR

Separator for error messages.

public static final String
ERROR_PREFIX

Prefix for errors.

public static final String
ERROR_SEPARATOR

Separator for errors.

public static final String
ERROR_SUFFIX

Suffix for errors.

private String
private final String
private final ParameterFormatter.MessagePatternAnalysis
public static final String
RECURSION_PREFIX

Prefix for recursion.

public static final String
RECURSION_SUFFIX

Suffix for recursion.

private static final long
private static final ThreadLocal<StringBuilder>
private final transient Throwable

Constructor Summary

AccessConstructor and Description
public
ParameterizedMessage(final String
a formatting pattern
pattern
,
final String[]
arguments to be formatted
args
,
final Throwable throwable)

Constructs an instance.

public
ParameterizedMessage(final String
a formatting pattern
pattern
,
final Object[]
arguments to be formatted
args
,
final Throwable throwable)

Constructs an instance.

public
ParameterizedMessage(final String
a formatting pattern
pattern
,
final Object...
arguments to be formatted
args
)

Constructor with a pattern and multiple arguments.

public
ParameterizedMessage(final String
a formatting pattern
pattern
,
final Object
an argument
arg
)

Constructor with a pattern and a single argument.

public
ParameterizedMessage(final String
a formatting pattern
pattern
,
final Object
the first argument
arg0
,
final Object
the second argument
arg1
)

Constructor with a pattern and two arguments.

Method Summary

Modifier and TypeMethod and Description
public static int
countArgumentPlaceholders(final String
the message pattern to be analyzed
pattern
)

Returns the number of argument placeholders.

public static String

Returns:

The String representation.
deepToString
(final Object
The object.
o
)

This method performs a deep toString of the given Object.

private static Throwable
determineThrowable(final Throwable throwable, final Object[] args, final ParameterFormatter.MessagePatternAnalysis analysis)

public boolean
equals(final Object
the reference object with which to compare.
object
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public static String
format(final String
a message pattern containing argument placeholders
pattern
,
final Object[]
arguments to be used to replace placeholders
args
)

Returns the formatted message.

public void
formatTo(final StringBuilder
the StringBuilder to write into
buffer
)

Implements org.apache.logging.log4j.util.StringBuilderFormattable.formatTo.

Writes a text representation of this object into the specified StringBuilder, ideally without allocating temporary objects.

public String

Returns:

the message formatting pattern
getFormat
()

Implements org.apache.logging.log4j.message.Message.getFormat.

Gets the format portion of the Message.

public String

Returns:

the formatted message
getFormattedMessage
()

Implements org.apache.logging.log4j.message.Message.getFormattedMessage.

Returns the formatted message.

public Object[]

Returns:

the message arguments
getParameters
()

Implements org.apache.logging.log4j.message.Message.getParameters.

Gets parameter values, if any.

public Throwable

Returns:

the Throwable provided along with the message
getThrowable
()

Implements org.apache.logging.log4j.message.Message.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
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public static String

Returns:

the identity string as also defined in Object.toString()
identityToString
(final Object
the Object that is to be converted into an identity string.
obj
)

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

private void
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

private void
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

argsback to summary
private transient Object[] args
ERROR_MSG_SEPARATORback to summary
public static final String ERROR_MSG_SEPARATOR

Separator for error messages.

ERROR_PREFIXback to summary
public static final String ERROR_PREFIX

Prefix for errors.

ERROR_SEPARATORback to summary
public static final String ERROR_SEPARATOR

Separator for errors.

ERROR_SUFFIXback to summary
public static final String ERROR_SUFFIX

Suffix for errors.

formattedMessageback to summary
private String formattedMessage
patternback to summary
private final String pattern
patternAnalysisback to summary
private final ParameterFormatter.MessagePatternAnalysis patternAnalysis
RECURSION_PREFIXback to summary
public static final String RECURSION_PREFIX

Prefix for recursion.

RECURSION_SUFFIXback to summary
public static final String RECURSION_SUFFIX

Suffix for recursion.

serialVersionUIDback to summary
private static final long serialVersionUID
STRING_BUILDER_HOLDERback to summary
private static final ThreadLocal<StringBuilder> STRING_BUILDER_HOLDER
throwableback to summary
private final transient Throwable throwable

Constructor Detail

ParameterizedMessageback to summary
public ParameterizedMessage(final String pattern, final String[] args, final 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:String

a formatting pattern

args:String[]

arguments to be formatted

throwable:Throwable

a Throwable

Annotations
@InlineMe
replacement:this(pattern, Arrays.stream(args).toArray(Object[]::new), throwable)
imports:Arrays
@Deprecated
ParameterizedMessageback to summary
public ParameterizedMessage(final String pattern, final Object[] args, final 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:String

a formatting pattern

args:Object[]

arguments to be formatted

throwable:Throwable

a Throwable

ParameterizedMessageback to summary
public ParameterizedMessage(final String pattern, final 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:String

a formatting pattern

args:Object[]

arguments to be formatted

ParameterizedMessageback to summary
public ParameterizedMessage(final String pattern, final 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:String

a formatting pattern

arg:Object

an argument

ParameterizedMessageback to summary
public ParameterizedMessage(final String pattern, final Object arg0, final 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:String

a formatting pattern

arg0:Object

the first argument

arg1:Object

the second argument

Method Detail

countArgumentPlaceholdersback to summary
public static int countArgumentPlaceholders(final String pattern)

Returns the number of argument placeholders.

Parameters
pattern:String

the message pattern to be analyzed

deepToStringback to summary
public static String deepToString(final 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:Object

The object.

Returns:String

The String representation.

determineThrowableback to summary
private static Throwable determineThrowable(final Throwable throwable, final Object[] args, final ParameterFormatter.MessagePatternAnalysis analysis)
equalsback to summary
public boolean equals(final Object object)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
object:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
formatback to summary
public static String format(final String pattern, final Object[] args)

Returns the formatted message.

Parameters
pattern:String

a message pattern containing argument placeholders

args:Object[]

arguments to be used to replace placeholders

formatToback to summary
public void formatTo(final StringBuilder buffer)

Implements org.apache.logging.log4j.util.StringBuilderFormattable.formatTo.

Doc from org.apache.logging.log4j.util.StringBuilderFormattable.formatTo.

Writes a text representation of this object into the specified StringBuilder, ideally without allocating temporary objects.

Parameters
buffer:StringBuilder

the StringBuilder to write into

Annotations
@Override
getFormatback to summary
public String getFormat()

Implements org.apache.logging.log4j.message.Message.getFormat.

Doc from org.apache.logging.log4j.message.Message.getFormat.

Gets the format portion of the Message.

Returns:String

the message formatting pattern

Annotations
@Override
getFormattedMessageback to summary
public String getFormattedMessage()

Implements org.apache.logging.log4j.message.Message.getFormattedMessage.

Returns the formatted message.

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

Returns:String

the formatted message

Annotations
@Override
getParametersback to summary
public Object[] getParameters()

Implements org.apache.logging.log4j.message.Message.getParameters.

Doc from org.apache.logging.log4j.message.Message.getParameters.

Gets parameter values, if any.

Returns:Object[]

the message arguments

Annotations
@Override
getThrowableback to summary
public Throwable getThrowable()

Implements org.apache.logging.log4j.message.Message.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
Returns:Throwable

the Throwable provided along with the message

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
identityToStringback to summary
public static String identityToString(final 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:Object

the Object that is to be converted into an identity string.

Returns:String

the identity string as also defined in Object.toString()

readObjectback to summary
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
writeObjectback to summary
private void writeObject(final ObjectOutputStream out) throws IOException