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

pack-priv final Class ParameterFormatter

extends Object
Class Inheritance
Imports
java.io.Serializable, java.time.ZoneId, java.time.format.DateTimeFormatter, java.util.*, org.apache.logging.log4j.util.StringBuilders

Supports parameter formatting as used in ParameterizedMessage and ReusableParameterizedMessage.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
private static final DateTimeFormatter
private static final char
private static final char
pack-priv static final String
ERROR_MSG_SEPARATOR

Separator for error messages.

pack-priv static final String
ERROR_PREFIX

Prefix for errors.

pack-priv static final String
ERROR_SEPARATOR

Separator for errors.

pack-priv static final String
ERROR_SUFFIX

Suffix for errors.

private static final char
pack-priv static final String
RECURSION_PREFIX

Prefix for recursion.

pack-priv static final String
RECURSION_SUFFIX

Suffix for recursion.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv static ParameterFormatter.MessagePatternAnalysis

Returns:

the analysis result
analyzePattern
(final String
a message pattern to be analyzed
pattern
,
final int
The number of arguments to be formatted. For instance, for a parametrized message containing 7 placeholders in the pattern and 4 arguments for formatting, analysis will only need to store the index of the first 4 placeholder characters. A negative value indicates no limit.
argCount
)

Analyzes – finds argument placeholder (i.e., "{}") occurrences, etc. – the given message pattern.

pack-priv static void
analyzePattern(final String
a message pattern to be analyzed
pattern
,
final int
The number of arguments to be formatted. For instance, for a parametrized message containing 7 placeholders in the pattern and 4 arguments for formatting, analysis will only need to store the index of the first 4 placeholder characters. A negative value indicates no limit.
argCount
,
final ParameterFormatter.MessagePatternAnalysis
an object to store the results
analysis
)

Analyzes – finds argument placeholder (i.e., "{}") occurrences, etc. – the given message pattern.

private static void
appendArray(final Object o, final StringBuilder str, final Set<Object> dejaVu, final Class<?> oClass)

private static void
appendCollection(final Object o, final StringBuilder str, final Set<Object> dejaVu)

Specialized handler for Collections.

private static boolean
appendDate(final Object o, final StringBuilder str)

private static void
appendMap(final Object o, final StringBuilder str, final Set<Object> dejaVu)

Specialized handler for Maps.

private static void
appendPotentiallyRecursiveValue(final Object o, final StringBuilder str, final Set<Object> dejaVu)

private static boolean
private static Set<Object>
cloneDejaVu(final Set<Object> dejaVu)

private static void
copyMessagePatternContainingEscapes(final StringBuilder buffer, final String pattern, final int startIndex, final int endIndex)

private static Set<Object>
pack-priv static String

Returns:

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

This method performs a deep toString of the given Object.

pack-priv static String

Returns:

the formatted message
format
(final String
a formatting pattern
pattern
,
final Object[]
arguments to be formatted
args
,
int argCount)

Format the following pattern using provided arguments.

pack-priv static void
formatMessage(final StringBuilder buffer, final String pattern, final Object[] args, final int argCount, final ParameterFormatter.MessagePatternAnalysis analysis)

pack-priv static void
formatMessageContainingEscapes(final StringBuilder buffer, final String pattern, final Object[] args, final int argCount, final ParameterFormatter.MessagePatternAnalysis analysis)

pack-priv static void
formatMessageContainingNoEscapes(final StringBuilder buffer, final String pattern, final Object[] args, final int argCount, final ParameterFormatter.MessagePatternAnalysis analysis)

private static Set<Object>
getOrCreateDejaVu(final Set<Object> dejaVu)

private static void
pack-priv 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 static boolean
isMaybeRecursive(final Object o)

Returns true if the specified object is an array, a Map or a Collection.

pack-priv static void
recursiveDeepToString(final Object
the Object to convert into a String
o
,
final StringBuilder
the StringBuilder that o will be appended to
str
)

This method performs a deep toString() of the given Object.

private static void
recursiveDeepToString(final Object
the Object to convert into a String
o
,
final StringBuilder
the StringBuilder that o will be appended to
str
,
final Set<Object>
a set of container objects directly or transitively containing o
dejaVu
)

This method performs a deep toString() of the given Object.

private static void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DATE_FORMATTERback to summary
private static final DateTimeFormatter DATE_FORMATTER
DELIM_STARTback to summary
private static final char DELIM_START
DELIM_STOPback to summary
private static final char DELIM_STOP
ERROR_MSG_SEPARATORback to summary
pack-priv static final String ERROR_MSG_SEPARATOR

Separator for error messages.

ERROR_PREFIXback to summary
pack-priv static final String ERROR_PREFIX

Prefix for errors.

ERROR_SEPARATORback to summary
pack-priv static final String ERROR_SEPARATOR

Separator for errors.

ERROR_SUFFIXback to summary
pack-priv static final String ERROR_SUFFIX

Suffix for errors.

ESCAPE_CHARback to summary
private static final char ESCAPE_CHAR
RECURSION_PREFIXback to summary
pack-priv static final String RECURSION_PREFIX

Prefix for recursion.

RECURSION_SUFFIXback to summary
pack-priv static final String RECURSION_SUFFIX

Suffix for recursion.

Constructor Detail

ParameterFormatterback to summary
private ParameterFormatter()

Method Detail

analyzePatternback to summary
pack-priv static ParameterFormatter.MessagePatternAnalysis analyzePattern(final String pattern, final int argCount)

Analyzes – finds argument placeholder (i.e., "{}") occurrences, etc. – the given message pattern.

Only "{}" strings are treated as argument placeholders. Escaped or incomplete argument placeholders will be ignored. Some invalid argument placeholder examples:

{ }
foo\{}
{bar
{buzz}
Parameters
pattern:String

a message pattern to be analyzed

argCount:int

The number of arguments to be formatted. For instance, for a parametrized message containing 7 placeholders in the pattern and 4 arguments for formatting, analysis will only need to store the index of the first 4 placeholder characters. A negative value indicates no limit.

Returns:ParameterFormatter.MessagePatternAnalysis

the analysis result

analyzePatternback to summary
pack-priv static void analyzePattern(final String pattern, final int argCount, final ParameterFormatter.MessagePatternAnalysis analysis)

Analyzes – finds argument placeholder (i.e., "{}") occurrences, etc. – the given message pattern.

Only "{}" strings are treated as argument placeholders. Escaped or incomplete argument placeholders will be ignored. Some invalid argument placeholder examples:

{ }
foo\{}
{bar
{buzz}
Parameters
pattern:String

a message pattern to be analyzed

argCount:int

The number of arguments to be formatted. For instance, for a parametrized message containing 7 placeholders in the pattern and 4 arguments for formatting, analysis will only need to store the index of the first 4 placeholder characters. A negative value indicates no limit.

analysis:ParameterFormatter.MessagePatternAnalysis

an object to store the results

appendArrayback to summary
private static void appendArray(final Object o, final StringBuilder str, final Set<Object> dejaVu, final Class<?> oClass)
appendCollectionback to summary
private static void appendCollection(final Object o, final StringBuilder str, final Set<Object> dejaVu)

Specialized handler for Collections.

appendDateback to summary
private static boolean appendDate(final Object o, final StringBuilder str)
appendMapback to summary
private static void appendMap(final Object o, final StringBuilder str, final Set<Object> dejaVu)

Specialized handler for Maps.

appendPotentiallyRecursiveValueback to summary
private static void appendPotentiallyRecursiveValue(final Object o, final StringBuilder str, final Set<Object> dejaVu)
appendSpecialTypesback to summary
private static boolean appendSpecialTypes(final Object o, final StringBuilder str)
cloneDejaVuback to summary
private static Set<Object> cloneDejaVu(final Set<Object> dejaVu)
copyMessagePatternContainingEscapesback to summary
private static void copyMessagePatternContainingEscapes(final StringBuilder buffer, final String pattern, final int startIndex, final int endIndex)
createDejaVuback to summary
private static Set<Object> createDejaVu()
deepToStringback to summary
pack-priv 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.

formatback to summary
pack-priv static String format(final String pattern, final Object[] args, int argCount)

Format the following pattern using provided arguments.

Parameters
pattern:String

a formatting pattern

args:Object[]

arguments to be formatted

Returns:String

the formatted message

formatMessageback to summary
pack-priv static void formatMessage(final StringBuilder buffer, final String pattern, final Object[] args, final int argCount, final ParameterFormatter.MessagePatternAnalysis analysis)
formatMessageContainingEscapesback to summary
pack-priv static void formatMessageContainingEscapes(final StringBuilder buffer, final String pattern, final Object[] args, final int argCount, final ParameterFormatter.MessagePatternAnalysis analysis)
formatMessageContainingNoEscapesback to summary
pack-priv static void formatMessageContainingNoEscapes(final StringBuilder buffer, final String pattern, final Object[] args, final int argCount, final ParameterFormatter.MessagePatternAnalysis analysis)
getOrCreateDejaVuback to summary
private static Set<Object> getOrCreateDejaVu(final Set<Object> dejaVu)
handleErrorInObjectToStringback to summary
private static void handleErrorInObjectToString(final Object o, final StringBuilder str, final Throwable t)
identityToStringback to summary
pack-priv 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()

isMaybeRecursiveback to summary
private static boolean isMaybeRecursive(final Object o)

Returns true if the specified object is an array, a Map or a Collection.

recursiveDeepToStringback to summary
pack-priv static void recursiveDeepToString(final Object o, final StringBuilder str)

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 to convert into a String

str:StringBuilder

the StringBuilder that o will be appended to

recursiveDeepToStringback to summary
private static void recursiveDeepToString(final Object o, final StringBuilder str, final Set<Object> dejaVu)

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.

dejaVu is used in case of those container types to prevent an endless recursion.

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 to convert into a String

str:StringBuilder

the StringBuilder that o will be appended to

dejaVu:Set<Object>

a set of container objects directly or transitively containing o

tryObjectToStringback to summary
private static void tryObjectToString(final Object o, final StringBuilder str)
org.apache.logging.log4j.message back to summary

pack-priv final Class ParameterFormatter.MessagePatternAnalysis

extends Object
implements Serializable
Class Inheritance
  • java.lang.Object
  • org.apache.logging.log4j.message.ParameterFormatter.MessagePatternAnalysis
All Implemented Interfaces
java.io.Serializable

See analyzePattern(String, int, MessagePatternAnalysis).

Field Summary

Modifier and TypeField and Description
pack-priv boolean
escapedCharFound

Flag indicating if an escaped (i.e., `\`-prefixed) character is found.

private static final int
PLACEHOLDER_CHAR_INDEX_BUFFER_INITIAL_SIZE

The size of the placeholderCharIndices buffer to be allocated if it is found to be null.

private static final int
PLACEHOLDER_CHAR_INDEX_BUFFER_SIZE_INCREMENT

The size placeholderCharIndices buffer will be extended with if it has found to be insufficient.

pack-priv int[]
placeholderCharIndices

The array of indices pointing to the first character of the found argument placeholder occurrences.

pack-priv int
placeholderCount

The total number of argument placeholder occurrences.

private static final long

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

escapedCharFoundback to summary
pack-priv boolean escapedCharFound

Flag indicating if an escaped (i.e., `\`-prefixed) character is found.

PLACEHOLDER_CHAR_INDEX_BUFFER_INITIAL_SIZEback to summary
private static final int PLACEHOLDER_CHAR_INDEX_BUFFER_INITIAL_SIZE

The size of the placeholderCharIndices buffer to be allocated if it is found to be null.

PLACEHOLDER_CHAR_INDEX_BUFFER_SIZE_INCREMENTback to summary
private static final int PLACEHOLDER_CHAR_INDEX_BUFFER_SIZE_INCREMENT

The size placeholderCharIndices buffer will be extended with if it has found to be insufficient.

placeholderCharIndicesback to summary
pack-priv int[] placeholderCharIndices

The array of indices pointing to the first character of the found argument placeholder occurrences.

placeholderCountback to summary
pack-priv int placeholderCount

The total number of argument placeholder occurrences.

serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

MessagePatternAnalysisback to summary
pack-priv MessagePatternAnalysis()

Method Detail

ensurePlaceholderCharIndicesCapacityback to summary
private void ensurePlaceholderCharIndicesCapacity(final int argCount)