public class ParameterizedMessage extends Object implements Message
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.
Modifier and Type | Field and 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 and Description |
---|
ParameterizedMessage(String messagePattern,
Object arg)
Constructor with a pattern and a single parameter.
|
ParameterizedMessage(String messagePattern,
Object[] arguments)
Constructs a ParameterizedMessage which contains the arguments converted to String as well as an optional
Throwable.
|
ParameterizedMessage(String messagePattern,
Object[] objectArgs,
Throwable throwable)
Creates a parameterized message.
|
ParameterizedMessage(String messagePattern,
Object arg1,
Object arg2)
Constructor with a pattern and two parameters.
|
ParameterizedMessage(String messagePattern,
String[] stringArgs,
Throwable throwable)
Creates a parameterized message.
|
Modifier and Type | Method and Description |
---|---|
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() |
public static final String RECURSION_PREFIX
public static final String RECURSION_SUFFIX
public static final String ERROR_PREFIX
public static final String ERROR_SEPARATOR
public static final String ERROR_MSG_SEPARATOR
public static final String ERROR_SUFFIX
public ParameterizedMessage(String messagePattern, String[] stringArgs, Throwable throwable)
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.public ParameterizedMessage(String messagePattern, Object[] objectArgs, Throwable throwable)
messagePattern
- The message "format" string. This will be a String containing "{}" placeholders
where parameters should be substituted.objectArgs
- The arguments for substitution.throwable
- A Throwable.public ParameterizedMessage(String messagePattern, Object[] arguments)
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 up getThrowable()
will return null even if the last argument was a Throwable!
messagePattern
- the message pattern that to be checked for placeholders.arguments
- the argument array to be converted.public ParameterizedMessage(String messagePattern, Object arg)
messagePattern
- The message pattern.arg
- The parameter.public ParameterizedMessage(String messagePattern, Object arg1, Object arg2)
messagePattern
- The message pattern.arg1
- The first parameter.arg2
- The second parameter.public String getFormattedMessage()
getFormattedMessage
in interface Message
public Object[] getParameters()
getParameters
in interface Message
public Throwable getThrowable()
getThrowable
in interface Message
protected String formatMessage(String msgPattern, String[] sArgs)
public static String format(String messagePattern, Object[] arguments)
messagePattern
- the message pattern containing placeholders.arguments
- the arguments to be used to replace placeholders.public static int countArgumentPlaceholders(String messagePattern)
messagePattern
- the message pattern to be analyzed.public static String deepToString(Object o)
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.
o
- The object.public static String identityToString(Object 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.)
obj
- the Object that is to be converted into an identity string.Copyright © 1999-2021 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.