Top Description Fields Constructors Methods
io.netty.util.internal.logging

public final Class MessageFormatter

extends Object
Class Inheritance
Imports
java.text.MessageFormat, java.util.HashSet, .Set

Formats messages according to very simple substitution rules. Substitutions can be made 1, 2 or more arguments.

For example,

MessageFormatter.format("Hi {}.", "there")

will return the string "Hi there.".

The {} pair is called the formatting anchor. It serves to designate the location where arguments need to be substituted within the message pattern.

In case your message contains the '{' or the '}' character, you do not have to do anything special unless the '}' character immediately follows '{'. For example,

MessageFormatter.format("Set {1,2,3} is not equal to {}.", "1,2");

will return the string "Set {1,2,3} is not equal to 1,2.".

If for whatever reason you need to place the string "{}" in the message without its formatting anchor meaning, then you need to escape the '{' character with '\', that is the backslash character. Only the '{' character should be escaped. There is no need to escape the '}' character. For example,

MessageFormatter.format("Set \\{} is not equal to {}.", "1,2");

will return the string "Set {} is not equal to 1,2.".

The escaping behavior just described can be overridden by escaping the escape character '\'. Calling

MessageFormatter.format("File name is C:\\\\{}.", "file.zip");

will return the string "File name is C:\file.zip".

The formatting conventions are different than those of MessageFormat which ships with the Java platform. This is justified by the fact that SLF4J's implementation is 10 times faster than that of MessageFormat. This local performance difference is both measurable and significant in the larger context of the complete logging processing chain.

See also format(String, Object), format(String, Object, Object) and arrayFormat(String, Object[]) methods for more details.

Field Summary

Modifier and TypeField and Description
private static final String
private static final char

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static FormattingTuple

Returns:

The formatted message
arrayFormat
(final String
The message pattern which will be parsed and formatted
messagePattern
,
final Object[]
An array of arguments to be substituted in place of formatting anchors
argArray
)

Same principle as the format(String, Object) and format(String, Object, Object) methods except that any number of arguments can be passed in an array.

private static void
booleanArrayAppend(StringBuilder sbuf, boolean[] a)

private static void
private static void
private static void
private static void
doubleArrayAppend(StringBuilder sbuf, double[] a)

private static void
public static FormattingTuple

Returns:

The formatted message
format
(String
The message pattern which will be parsed and formatted
messagePattern
,
Object
The argument to be substituted in place of the formatting anchor
arg
)

Performs single argument substitution for the 'messagePattern' passed as parameter.

public static FormattingTuple

Returns:

The formatted message
format
(final String
The message pattern which will be parsed and formatted
messagePattern
,
Object
The argument to be substituted in place of the first formatting anchor
argA
,
Object
The argument to be substituted in place of the second formatting anchor
argB
)

Performs a two argument substitution for the 'messagePattern' passed as parameter.

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

Field Detail

DELIM_STRback to summary
private static final String DELIM_STR
ESCAPE_CHARback to summary
private static final char ESCAPE_CHAR

Constructor Detail

MessageFormatterback to summary
private MessageFormatter()

Method Detail

arrayFormatback to summary
public static FormattingTuple arrayFormat(final String messagePattern, final Object[] argArray)

Same principle as the format(String, Object) and format(String, Object, Object) methods except that any number of arguments can be passed in an array.

Parameters
messagePattern:String

The message pattern which will be parsed and formatted

argArray:Object[]

An array of arguments to be substituted in place of formatting anchors

Returns:FormattingTuple

The formatted message

booleanArrayAppendback to summary
private static void booleanArrayAppend(StringBuilder sbuf, boolean[] a)
byteArrayAppendback to summary
private static void byteArrayAppend(StringBuilder sbuf, byte[] a)
charArrayAppendback to summary
private static void charArrayAppend(StringBuilder sbuf, char[] a)
deeplyAppendParameterback to summary
private static void deeplyAppendParameter(StringBuilder sbuf, Object o, Set<Object[]> seenSet)
doubleArrayAppendback to summary
private static void doubleArrayAppend(StringBuilder sbuf, double[] a)
floatArrayAppendback to summary
private static void floatArrayAppend(StringBuilder sbuf, float[] a)
formatback to summary
public static FormattingTuple format(String messagePattern, Object arg)

Performs single argument substitution for the 'messagePattern' passed as parameter.

For example,

MessageFormatter.format("Hi {}.", "there");

will return the string "Hi there.".

Parameters
messagePattern:String

The message pattern which will be parsed and formatted

arg:Object

The argument to be substituted in place of the formatting anchor

Returns:FormattingTuple

The formatted message

formatback to summary
public static FormattingTuple format(final String messagePattern, Object argA, Object argB)

Performs a two argument substitution for the 'messagePattern' passed as parameter.

For example,

MessageFormatter.format("Hi {}. My name is {}.", "Alice", "Bob");

will return the string "Hi Alice. My name is Bob.".

Parameters
messagePattern:String

The message pattern which will be parsed and formatted

argA:Object

The argument to be substituted in place of the first formatting anchor

argB:Object

The argument to be substituted in place of the second formatting anchor

Returns:FormattingTuple

The formatted message

intArrayAppendback to summary
private static void intArrayAppend(StringBuilder sbuf, int[] a)
longArrayAppendback to summary
private static void longArrayAppend(StringBuilder sbuf, long[] a)
objectArrayAppendback to summary
private static void objectArrayAppend(StringBuilder sbuf, Object[] a, Set<Object[]> seenSet)
safeObjectAppendback to summary
private static void safeObjectAppend(StringBuilder sbuf, Object o)
shortArrayAppendback to summary
private static void shortArrayAppend(StringBuilder sbuf, short[] a)