org.apache.logging.log4j.message
Class ParameterizedMessage

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

public class ParameterizedMessage
extends Object
implements Message

Handles messages that consist of a format string containing '{}' to represent each replaceable token, and the parameters.

This class was originally written for Lillith (http://mac.freshmeat.net/projects/lilith-viewer) 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
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
ParameterizedMessage(String messagePattern, Object arg)
          Constructor with a pattern and a single parameter.
ParameterizedMessage(String messagePattern, Object[] arguments)
          This method returns a ParameterizedMessage which contains the arguments converted to String as well as an optional Throwable.
ParameterizedMessage(String messagePattern, Object[] arguments, Throwable throwable)
           
ParameterizedMessage(String messagePattern, Object arg1, Object arg2)
          Constructor with a pattern and two parameters.
ParameterizedMessage(String messagePattern, String[] stringArgs, Throwable throwable)
          Create the parameterizedMessage.
 
Method Summary
static int countArgumentPlaceholders(String messagePattern)
          Counts the number of unescaped placeholders in the given messagePattern.
static String deepToString(Object o)
          This method performs a deep toString of the given Object.
 boolean equals(Object o)
           
static String format(String messagePattern, Object[] arguments)
          Replace placeholders in the given messagePattern with arguments.
protected  String formatMessage(String msgPattern, String[] sArgs)
           
 String getFormat()
          Returns the message pattern.
 String getFormattedMessage()
          Returns the formatted message.
 Object[] getParameters()
          Returns the message parameters.
 Throwable getThrowable()
          Returns the Throwable that was given as the last argument, if any.
 int hashCode()
           
static String identityToString(Object obj)
          This method returns the same as if Object.toString() would not have been overridden in obj.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

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

public ParameterizedMessage(String messagePattern,
                            String[] stringArgs,
                            Throwable throwable)
Create the parameterizedMessage.

Parameters:
messagePattern - The message "format" string. This will be a String containing "{}" placeholders where parameters should be substituted.
stringArgs - The arguments for substitution.
throwable - A Throwable.

ParameterizedMessage

public ParameterizedMessage(String messagePattern,
                            Object[] arguments,
                            Throwable throwable)

ParameterizedMessage

public ParameterizedMessage(String messagePattern,
                            Object[] arguments)

This method returns 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 ParameterizedMessage.getThrowable() and won't be contained in the created String[].
If it is used up ParameterizedMessage.getThrowable() 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(String messagePattern,
                            Object arg)
Constructor with a pattern and a single parameter.

Parameters:
messagePattern - The message pattern.
arg - The parameter.

ParameterizedMessage

public ParameterizedMessage(String messagePattern,
                            Object arg1,
                            Object arg2)
Constructor with a pattern and two parameters.

Parameters:
messagePattern - The message pattern.
arg1 - The first parameter.
arg2 - The second parameter.
Method Detail

getFormattedMessage

public String getFormattedMessage()
Returns the formatted message.

Specified by:
getFormattedMessage in interface Message
Returns:
the formatted message.

getFormat

public String getFormat()
Returns the message pattern.

Specified by:
getFormat in interface Message
Returns:
the message pattern.

getParameters

public Object[] getParameters()
Returns the message parameters.

Specified by:
getParameters in interface Message
Returns:
the message parameters.

getThrowable

public 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 interface Message
Returns:
the Throwable, if any.

formatMessage

protected String formatMessage(String msgPattern,
                               String[] sArgs)

equals

public boolean equals(Object o)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

format

public static String format(String messagePattern,
                            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.

countArgumentPlaceholders

public static int countArgumentPlaceholders(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 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 JavaTM 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


Copyright © 1999-2013 Apache Software Foundation. All Rights Reserved.
Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, the Apache Logging project logo, and the Apache Log4j logo are trademarks of The Apache Software Foundation.