<div dir="ltr">And I just found out that it would be more appropriate to use Arrays.deepToString((Object[]) o); in case of Object[].<br><br><div class="gmail_quote">On Fri, Aug 1, 2008 at 2:40 PM, Jörn Huxhorn <span dir="ltr"><<a href="mailto:jhuxhorn@googlemail.com">jhuxhorn@googlemail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div dir="ltr">Hi Ceki.<br><br>You forgot the double[] in your last commit.<br><br>Regards, Joern.<div>
<div></div><div class="Wj3C7c"><br><br><div class="gmail_quote">On Thu, Jul 31, 2008 at 10:51 PM, <span dir="ltr"><<a href="mailto:ceki@slf4j.org" target="_blank">ceki@slf4j.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Author: ceki<br>
Date: Thu Jul 31 22:51:04 2008<br>
New Revision: 1086<br>
<br>
Modified:<br>
slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/MessageFormatter.java<br>
slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/MessageFormatterTest.java<br>
<br>
Log:<br>
- add support for array values in parameters<br>
<br>
For example,<br>
<br>
log.debug("a:{},i:{}", "A", new int[] {1, 2}});<br>
will print as "a:A,i:[1, 2]" instead of "a:A,i:[I@6ca1c" as previously<br>
<br>
log.debug("a:{},b:{},i:{},", new Object[] {"A", "B", new int[] {1, 2}});<br>
will print as "a:A,b:B,i:[1, 2]" instead of "a:A,b:B,i:[I@90c78" as previously<br>
<br>
This enhancement was proposed by "lizongbo"<br>
<br>
Modified: slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/MessageFormatter.java<br>
==============================================================================<br>
--- slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/MessageFormatter.java (original)<br>
+++ slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/MessageFormatter.java Thu Jul 31 22:51:04 2008<br>
@@ -24,12 +24,20 @@<br>
<br>
package org.slf4j.helpers;<br>
<br>
+import java.util.Arrays;<br>
+<br>
+// contributors: lizongbo<br>
+<br>
/**<br>
* Formats messages according to very simple substitution rules. Substitutions<br>
* can be made 1, 2 or more arguments.<br>
* <p><br>
* For example,<br>
- * <pre>MessageFormatter.format(&quot;Hi {}.&quot;, &quot;there&quot;);</pre><br>
+ *<br>
+ * <pre><br>
+ * MessageFormatter.format(&quot;Hi {}.&quot;, &quot;there&quot;);<br>
+ * </pre><br>
+ *<br>
* will return the string "Hi there.".<br>
* <p><br>
* The {} pair is called the <em>formatting anchor</em>. It serves to<br>
@@ -40,14 +48,22 @@<br>
* pattern itself but do not want them to be interpreted as a formatting<br>
* anchors, you can escape the '{' character with '\', that is the backslash<br>
* character. Only the '{' character should be escaped. There is no need to<br>
- * escape the '}' character. For example,<br>
- * <pre>MessageFormatter.format(&quot;Set \\{1,2,3} is not equal to {}.&quot;, &quot;1,2&quot;);</pre><br>
- * will return the string "Set {1,2,3} is not equal to 1,2.".<br>
+ * escape the '}' character. For example,<br>
+ *<br>
+ * <pre><br>
+ * MessageFormatter.format(&quot;Set \\{1,2,3} is not equal to {}.&quot;, &quot;1,2&quot;);<br>
+ * </pre><br>
+ *<br>
+ * will return the string "Set {1,2,3} is not equal to 1,2.".<br>
*<br>
* <p><br>
- * The escaping behavior just described can be overridden by<br>
- * escaping the escape character '\'. Calling<br>
- * <pre>MessageFormatter.format(&quot;File name is C:\\\\{}.&quot;, &quot;file.zip&quot;);</pre><br>
+ * The escaping behavior just described can be overridden by escaping the escape<br>
+ * character '\'. Calling<br>
+ *<br>
+ * <pre><br>
+ * MessageFormatter.format(&quot;File name is C:\\\\{}.&quot;, &quot;file.zip&quot;);<br>
+ * </pre><br>
+ *<br>
* will return the string "File name is C:\file.zip".<br>
*<br>
* <p><br>
@@ -60,7 +76,7 @@<br>
static final char DELIM_START = '{';<br>
static final char DELIM_STOP = '}';<br>
private static final char ESCAPE_CHAR = '\\';<br>
-<br>
+<br>
/**<br>
* Performs single argument substitution for the 'messagePattern' passed as<br>
* parameter.<br>
@@ -75,9 +91,10 @@<br>
* <p><br>
*<br>
* @param messagePattern<br>
- * The message pattern which will be parsed and formatted<br>
+ * The message pattern which will be parsed and formatted<br>
* @param argument<br>
- * The argument to be substituted in place of the formatting anchor<br>
+ * The argument to be substituted in place of the formatting<br>
+ * anchor<br>
* @return The formatted message<br>
*/<br>
public static String format(String messagePattern, Object arg) {<br>
@@ -98,13 +115,13 @@<br>
* will return the string "Hi Alice. My name is Bob.".<br>
*<br>
* @param messagePattern<br>
- * The message pattern which will be parsed and formatted<br>
+ * The message pattern which will be parsed and formatted<br>
* @param arg1<br>
- * The argument to be substituted in place of the first formatting<br>
- * anchor<br>
+ * The argument to be substituted in place of the first<br>
+ * formatting anchor<br>
* @param arg2<br>
- * The argument to be substituted in place of the second formatting<br>
- * anchor<br>
+ * The argument to be substituted in place of the second<br>
+ * formatting anchor<br>
* @return The formatted message<br>
*/<br>
public static String format(String messagePattern, Object arg1, Object arg2) {<br>
@@ -117,10 +134,10 @@<br>
* arguments can be passed in an array.<br>
*<br>
* @param messagePattern<br>
- * The message pattern which will be parsed and formatted<br>
+ * The message pattern which will be parsed and formatted<br>
* @param argArray<br>
- * An array of arguments to be substituted in place of formatting<br>
- * anchors<br>
+ * An array of arguments to be substituted in place of<br>
+ * formatting anchors<br>
* @return The formatted message<br>
*/<br>
public static String arrayFormat(String messagePattern, Object[] argArray) {<br>
@@ -131,10 +148,10 @@<br>
int len = messagePattern.length();<br>
int j = messagePattern.indexOf(DELIM_START);<br>
<br>
- if(argArray == null) {<br>
+ if (argArray == null) {<br>
return messagePattern;<br>
}<br>
-<br>
+<br>
StringBuffer sbuf = new StringBuffer(messagePattern.length() + 50);<br>
<br>
for (int L = 0; L < argArray.length; L++) {<br>
@@ -154,7 +171,7 @@<br>
char delimStop = messagePattern.charAt(j + 1);<br>
<br>
if (isEscapedDelimeter(messagePattern, j)) {<br>
- if(!isDoubleEscaped(messagePattern, j)) {<br>
+ if (!isDoubleEscaped(messagePattern, j)) {<br>
L--; // DELIM_START was escaped, thus should not be incremented<br>
sbuf.append(messagePattern.substring(i, j - 1));<br>
sbuf.append(DELIM_START);<br>
@@ -163,8 +180,9 @@<br>
// The escape character preceding the delemiter start is<br>
// itself escaped: "abc x:\\{}"<br>
// we have to consume one backward slash<br>
- sbuf.append(messagePattern.substring(i, j-1));<br>
- sbuf.append(argArray[L]);<br>
+ sbuf.append(messagePattern.substring(i, j - 1));<br>
+ appendParameter(sbuf, argArray[L]);<br>
+ // sbuf.append(argArray[L]);<br>
i = j + 2;<br>
}<br>
} else if ((delimStop != DELIM_STOP)) {<br>
@@ -174,7 +192,7 @@<br>
} else {<br>
// normal case<br>
sbuf.append(messagePattern.substring(i, j));<br>
- sbuf.append(argArray[L]);<br>
+ appendParameter(sbuf, argArray[L]);<br>
i = j + 2;<br>
}<br>
}<br>
@@ -206,4 +224,31 @@<br>
return false;<br>
}<br>
}<br>
+<br>
+ // special treatment of array values was suggested by 'lizongbo'<br>
+ private static void appendParameter(StringBuffer sbuf, Object o) {<br>
+ if (o != null && o.getClass().isArray()) {<br>
+ // check for primitive arrays because they unfortunately<br>
+ // cannot be cast to Object[]<br>
+ if (o instanceof boolean[]) {<br>
+ sbuf.append(Arrays.toString((boolean[]) o));<br>
+ } else if (o instanceof byte[]) {<br>
+ sbuf.append(Arrays.toString((byte[]) o));<br>
+ } else if (o instanceof char[]) {<br>
+ sbuf.append(Arrays.toString((char[]) o));<br>
+ } else if (o instanceof short[]) {<br>
+ sbuf.append(Arrays.toString((short[]) o));<br>
+ } else if (o instanceof int[]) {<br>
+ sbuf.append(Arrays.toString((int[]) o));<br>
+ } else if (o instanceof long[]) {<br>
+ sbuf.append(Arrays.toString((long[]) o));<br>
+ } else if (o instanceof float[]) {<br>
+ sbuf.append(Arrays.toString((float[]) o));<br>
+ } else {<br>
+ sbuf.append(Arrays.toString((Object[]) o));<br>
+ }<br>
+ } else {<br>
+ sbuf.append(o);<br>
+ }<br>
+ }<br>
}<br>
<br>
Modified: slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/MessageFormatterTest.java<br>
==============================================================================<br>
--- slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/MessageFormatterTest.java (original)<br>
+++ slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/MessageFormatterTest.java Thu Jul 31 22:51:04 2008<br>
@@ -33,56 +33,59 @@<br>
<br>
package org.slf4j.helpers;<br>
<br>
+import java.util.Arrays;<br>
+<br>
import org.slf4j.helpers.MessageFormatter;<br>
<br>
import junit.framework.TestCase;<br>
<br>
-<br>
/**<br>
* @author Ceki Gulcu<br>
- *<br>
+ *<br>
*/<br>
public class MessageFormatterTest extends TestCase {<br>
-<br>
+<br>
Integer i1 = new Integer(1);<br>
Integer i2 = new Integer(2);<br>
Integer i3 = new Integer(3);<br>
-<br>
+<br>
public void testNull() {<br>
String result;<br>
result = MessageFormatter.format(null, i1);<br>
assertEquals(null, result);<br>
}<br>
-<br>
+<br>
public void testNullParam() {<br>
String result;<br>
-<br>
+<br>
result = MessageFormatter.format("Value is {}.", null);<br>
assertEquals("Value is null.", result);<br>
-<br>
+<br>
result = MessageFormatter.format("Val1 is {}, val2 is {}.", null, null);<br>
assertEquals("Val1 is null, val2 is null.", result);<br>
-<br>
+<br>
result = MessageFormatter.format("Val1 is {}, val2 is {}.", i1, null);<br>
assertEquals("Val1 is 1, val2 is null.", result);<br>
-<br>
+<br>
result = MessageFormatter.format("Val1 is {}, val2 is {}.", null, i2);<br>
assertEquals("Val1 is null, val2 is 2.", result);<br>
-<br>
- result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}", new Integer[]{null, null, null});<br>
+<br>
+ result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}",<br>
+ new Integer[] { null, null, null });<br>
assertEquals("Val1 is null, val2 is null, val3 is null", result);<br>
-<br>
- result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}", new Integer[]{null, i2, i3});<br>
+<br>
+ result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}",<br>
+ new Integer[] { null, i2, i3 });<br>
assertEquals("Val1 is null, val2 is 2, val3 is 3", result);<br>
-<br>
- result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}", new Integer[]{null, null, i3});<br>
+<br>
+ result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}",<br>
+ new Integer[] { null, null, i3 });<br>
assertEquals("Val1 is null, val2 is null, val3 is 3", result);<br>
}<br>
-<br>
-<br>
+<br>
public void testOneParameter() {<br>
String result;<br>
-<br>
+<br>
result = MessageFormatter.format("Value is {}.", i3);<br>
assertEquals("Value is 3.", result);<br>
<br>
@@ -94,13 +97,13 @@<br>
<br>
result = MessageFormatter.format("No subst", i3);<br>
assertEquals("No subst", result);<br>
-<br>
+<br>
result = MessageFormatter.format("Incorrect {subst", i3);<br>
assertEquals("Incorrect {subst", result);<br>
-<br>
+<br>
result = MessageFormatter.format("Value is \\{bla} {}", i3);<br>
assertEquals("Value is {bla} 3", result);<br>
-<br>
+<br>
result = MessageFormatter.format("Escaped \\{} subst", i3);<br>
assertEquals("Escaped {} subst", result);<br>
<br>
@@ -109,51 +112,52 @@<br>
<br>
result = MessageFormatter.format("\\{}Escaped", i3);<br>
assertEquals("{}Escaped", result);<br>
-<br>
+<br>
result = MessageFormatter.format("File name is \\{{}}.", "App folder.zip");<br>
assertEquals("File name is {App folder.zip}.", result);<br>
-<br>
+<br>
// escaping the escape character<br>
- result = MessageFormatter.format("File name is C:\\\\{}.", "App folder.zip");<br>
+ result = MessageFormatter<br>
+ .format("File name is C:\\\\{}.", "App folder.zip");<br>
assertEquals("File name is C:\\App folder.zip.", result);<br>
}<br>
-<br>
+<br>
public void testTwoParameters() {<br>
String result;<br>
<br>
-<br>
result = MessageFormatter.format("Value {} is smaller than {}.", i1, i2);<br>
assertEquals("Value 1 is smaller than 2.", result);<br>
-<br>
+<br>
result = MessageFormatter.format("Value {} is smaller than {}", i1, i2);<br>
assertEquals("Value 1 is smaller than 2", result);<br>
-<br>
+<br>
result = MessageFormatter.format("{}{}", i1, i2);<br>
assertEquals("12", result);<br>
-<br>
+<br>
result = MessageFormatter.format("Val1={}, Val2={", i1, i2);<br>
assertEquals("Val1=1, Val2={", result);<br>
<br>
result = MessageFormatter.format("Value {} is smaller than \\{}", i1, i2);<br>
assertEquals("Value 1 is smaller than {}", result);<br>
-<br>
- result = MessageFormatter.format("Value {} is smaller than \\{} tail", i1, i2);<br>
- assertEquals("Value 1 is smaller than {} tail", result);<br>
+<br>
+ result = MessageFormatter.format("Value {} is smaller than \\{} tail", i1,<br>
+ i2);<br>
+ assertEquals("Value 1 is smaller than {} tail", result);<br>
<br>
result = MessageFormatter.format("Value {} is smaller than \\{", i1, i2);<br>
- assertEquals("Value 1 is smaller than \\{", result);<br>
-<br>
- result = MessageFormatter.format("Value {} is smaller than \\{tail", i1, i2);<br>
- assertEquals("Value 1 is smaller than {tail", result);<br>
-<br>
-<br>
+ assertEquals("Value 1 is smaller than \\{", result);<br>
+<br>
+ result = MessageFormatter<br>
+ .format("Value {} is smaller than \\{tail", i1, i2);<br>
+ assertEquals("Value 1 is smaller than {tail", result);<br>
+<br>
result = MessageFormatter.format("Value \\{} is smaller than {}", i1, i2);<br>
- assertEquals("Value {} is smaller than 1", result);<br>
+ assertEquals("Value {} is smaller than 1", result);<br>
}<br>
-<br>
+<br>
public void testNullArray() {<br>
String result;<br>
-<br>
+<br>
String msg0 = "msg0";<br>
String msg1 = "msg1 {}";<br>
String msg2 = "msg2 {} {}";<br>
@@ -163,44 +167,69 @@<br>
<br>
result = MessageFormatter.arrayFormat(msg0, args);<br>
assertEquals(msg0, result);<br>
-<br>
+<br>
result = MessageFormatter.arrayFormat(msg1, args);<br>
assertEquals(msg1, result);<br>
-<br>
+<br>
result = MessageFormatter.arrayFormat(msg2, args);<br>
assertEquals(msg2, result);<br>
-<br>
+<br>
result = MessageFormatter.arrayFormat(msg3, args);<br>
assertEquals(msg3, result);<br>
}<br>
- public void testArray() {<br>
+<br>
+ // tests the case when the parameters are supplied in a single array<br>
+ public void testArrayFormat() {<br>
String result;<br>
<br>
- Integer[] ia = new Integer[] {i1, i2, i3};<br>
+ Integer[] ia = new Integer[] { i1, i2, i3 };<br>
<br>
- result = MessageFormatter.arrayFormat("Value {} is smaller than {} and {}.", ia);<br>
+ result = MessageFormatter.arrayFormat(<br>
+ "Value {} is smaller than {} and {}.", ia);<br>
assertEquals("Value 1 is smaller than 2 and 3.", result);<br>
-<br>
+<br>
result = MessageFormatter.arrayFormat("{}{}{}", ia);<br>
assertEquals("123", result);<br>
-<br>
+<br>
result = MessageFormatter.arrayFormat("Value {} is smaller than {}.", ia);<br>
assertEquals("Value 1 is smaller than 2.", result);<br>
-<br>
+<br>
result = MessageFormatter.arrayFormat("Value {} is smaller than {}", ia);<br>
assertEquals("Value 1 is smaller than 2", result);<br>
-<br>
+<br>
result = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia);<br>
assertEquals("Val=1, {, Val={}", result);<br>
-<br>
+<br>
result = MessageFormatter.arrayFormat("Val={}, \\{, Val={}", ia);<br>
assertEquals("Val=1, {, Val=2", result);<br>
-<br>
-<br>
+<br>
result = MessageFormatter.arrayFormat("Val1={}, Val2={", ia);<br>
assertEquals("Val1=1, Val2={", result);<br>
-<br>
+ }<br>
+<br>
+ public void testArrayValues() {<br>
+<br>
+ String result;<br>
+<br>
+ Integer p0 = i1;<br>
+ Integer[] p1 = new Integer[] { i2, i3 };<br>
+<br>
+ System.out.println("[" + Arrays.toString(new int[] { 1, 2 }) + "]");<br>
+<br>
+ result = MessageFormatter.format("{}{}", p0, p1);<br>
+ assertEquals(p0 + Arrays.toString(p1), result);<br>
+<br>
+ {<br>
+ Object[] pa = new Object[] { "a", p1 };<br>
+ result = MessageFormatter.arrayFormat("{}{}", pa);<br>
+ assertEquals("a" + Arrays.toString(p1), result);<br>
+ }<br>
<br>
+ {<br>
+ Object[] pa = new Object[] { "a", new int[] { 1, 2 } };<br>
+ result = MessageFormatter.arrayFormat("{}{}", pa);<br>
+ assertEquals("a" + Arrays.toString(new int[] { 1, 2 }), result);<br>
+ }<br>
}<br>
<br>
}<br>
_______________________________________________<br>
dev mailing list<br>
<a href="mailto:dev@slf4j.org" target="_blank">dev@slf4j.org</a><br>
<a href="http://www.slf4j.org/mailman/listinfo/dev" target="_blank">http://www.slf4j.org/mailman/listinfo/dev</a><br>
</blockquote></div><br></div></div></div>
</blockquote></div><br></div>