ceki 2003/09/19 12:54:12
Modified: tests/src/java/org/apache/log4j/pattern
PatternParserTest.java
examples/pattern MyPatternLayout.java
docs HISTORY
src/java/org/apache/log4j/pattern PatternParser.java
tests/input patternLayout14.properties
Log:
- Couple bug fixes in PatternParser.
- PatternLayout now recognized the pattern words '"logger", "class",
"file", "line", "message", "level", "relative", "thread"
"ndc", "mdc" which aare synonyms respectively for 'c', 'C', 'F', 'L',
'p', 'r', 't', 'x' and 'X'.
Revision Changes Path
1.2 +72 -12 jakarta-log4j/tests/src/java/org/apache/log4j/pattern/PatternParserTest.java
Index: PatternParserTest.java
===================================================================
RCS file: /home/cvs/jakarta-log4j/tests/src/java/org/apache/log4j/pattern/PatternParserTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- PatternParserTest.java 18 Sep 2003 21:26:55 -0000 1.1
+++ PatternParserTest.java 19 Sep 2003 19:54:11 -0000 1.2
@@ -53,6 +53,7 @@
import junit.framework.TestCase;
import junit.framework.TestSuite;
+import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggingEvent;
@@ -70,10 +71,17 @@
*/
public class PatternParserTest extends TestCase {
public CharArrayWriter charArrayWriter = new CharArrayWriter(1024);
- Logger logger = Logger.getLogger(PatternParserTest.class);
+ Logger logger = Logger.getLogger("org.foobar");
+ LoggingEvent event;
+ long now;
public PatternParserTest(String name) {
super(name);
+ now = System.currentTimeMillis() + 13;
+
+ event =
+ new LoggingEvent(
+ Logger.class.getName(), logger, now, Level.INFO, "msg 1", null);
}
public void setUp() {
@@ -88,7 +96,7 @@
PatternConverter c = head;
while (c != null) {
- System.out.println("pc "+c);
+ System.out.println("pc " + c);
c.format(charArrayWriter, event);
c = c.next;
}
@@ -97,26 +105,78 @@
}
public void testNewWord() throws Exception {
- PatternParser patternParser = new PatternParser("%zum343");
+ PatternParser patternParser = new PatternParser("%z343");
HashMap ruleRegistry = new HashMap(5);
- ruleRegistry.put("zum343", Num343PatternConverter.class.getName());
+ ruleRegistry.put("z343", Num343PatternConverter.class.getName());
+ patternParser.setConverterRegistry(ruleRegistry);
+
+ PatternConverter head = patternParser.parse();
+
+ String result = convert(event, head);
+ System.out.println("Result is[" + result + "]");
+ assertEquals("343", result);
+ }
+
+ /* Test whether words starting with the letter 'n' are treated differently,
+ * which was previously the case by mistake.
+ */
+ public void testNewWord2() throws Exception {
+ PatternParser patternParser = new PatternParser("%n343");
+ HashMap ruleRegistry = new HashMap(5);
+
+ ruleRegistry.put("n343", Num343PatternConverter.class.getName());
patternParser.setConverterRegistry(ruleRegistry);
PatternConverter head = patternParser.parse();
- LoggingEvent event =
- new LoggingEvent(
- Logger.class.getName(), logger, Level.DEBUG, "msg 1", null);
String result = convert(event, head);
- System.out.println("Resuls is["+result+"]");
+ System.out.println("Result is[" + result + "]");
assertEquals("343", result);
}
- public static Test suite() {
- TestSuite suite = new TestSuite();
- suite.addTest(new PatternParserTest("testNewWord"));
+ public void testBogusWord1() throws Exception {
+ PatternParser patternParser = new PatternParser("%, foobar");
+ PatternConverter head = patternParser.parse();
- return suite;
+ String result = convert(event, head);
+ System.out.println("Result is[" + result + "]");
+ assertEquals("%, foobar", result);
}
+
+ public void testBogusWord2() throws Exception {
+ PatternParser patternParser = new PatternParser("xyz %, foobar");
+ PatternConverter head = patternParser.parse();
+
+ String result = convert(event, head);
+ System.out.println("Result is[" + result + "]");
+ assertEquals("xyz %, foobar", result);
+ }
+
+ public void testBasic1() throws Exception {
+ PatternParser patternParser = new PatternParser("hello %-5level - %m%n");
+ PatternConverter head = patternParser.parse();
+
+ String result = convert(event, head);
+ System.out.println("Result is[" + result + "]");
+ assertEquals("hello INFO - msg 1" + Layout.LINE_SEP, result);
+ }
+
+ public void testBasic2() throws Exception {
+ PatternParser patternParser =
+ new PatternParser("%relative %-5level [%thread] %logger - %m%n");
+ PatternConverter head = patternParser.parse();
+
+ String result = convert(event, head);
+ long expectedRelativeTime = now - LoggingEvent.getStartTime();
+ System.out.println("Result is[" + result + "]");
+ assertEquals(expectedRelativeTime + " INFO [main] "+logger.getName()+" - msg 1" +
Layout.LINE_SEP, result);
+ }
+
+// public static Test suite() {
+// TestSuite suite = new TestSuite();
+// suite.addTest(new PatternParserTest("testBasic2"));
+//
+// return suite;
+// }
}
1.2 +5 -4 jakarta-log4j/examples/pattern/MyPatternLayout.java
Index: MyPatternLayout.java
===================================================================
RCS file: /home/cvs/jakarta-log4j/examples/pattern/MyPatternLayout.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MyPatternLayout.java 18 Sep 2003 21:26:54 -0000 1.1
+++ MyPatternLayout.java 19 Sep 2003 19:54:12 -0000 1.2
@@ -57,10 +57,11 @@
/**
*
* Example showing how to extend PatternLayout to recognize additional
- * conversion characters.
+ * conversion words without adding.
*
- * <p>In this case MyPatternLayout recognizes %# conversion pattern. It outputs
- * the value of an internal counter which is also incremented at each call.
+ * <p>In this case MyPatternLayout recognizes %counter conversion word.
+ * It outputs the value of an internal counter which is also incremented at
+ * each call.
*
* @see org.apache.log4j.PatternLayout
* @author Anders Kristensen
@@ -81,7 +82,7 @@
you change the parameters of the PatternLayout instance.
*/
public void activateOptions() {
- this.addConversionRule("#", CountingPatternConverter.class.getName());
+ this.addConversionRule("counter", CountingPatternConverter.class.getName());
super.activateOptions();
}
1.103 +3 -0 jakarta-log4j/docs/HISTORY
Index: HISTORY
===================================================================
RCS file: /home/cvs/jakarta-log4j/docs/HISTORY,v
retrieving revision 1.102
retrieving revision 1.103
diff -u -r1.102 -r1.103
--- HISTORY 11 Sep 2003 08:49:46 -0000 1.102
+++ HISTORY 19 Sep 2003 19:54:12 -0000 1.103
@@ -33,6 +33,9 @@
type (like XML for example).
- Fixed bug #23096. NullAppender getInstance method is now declared as a static.
+
+ - Fixed bug #10706. The %X pattern without a key no longer causes a NullPointer
+ exception,
February 19th, 2003
1.6 +42 -35 jakarta-log4j/src/java/org/apache/log4j/pattern/PatternParser.java
Index: PatternParser.java
===================================================================
RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/pattern/PatternParser.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- PatternParser.java 18 Sep 2003 21:26:55 -0000 1.5
+++ PatternParser.java 19 Sep 2003 19:54:12 -0000 1.6
@@ -49,7 +49,6 @@
package org.apache.log4j.pattern;
-import org.apache.log4j.Layout;
import org.apache.log4j.Logger;
import org.apache.log4j.helpers.AbsoluteTimeDateFormat;
import org.apache.log4j.helpers.LogLog;
@@ -83,38 +82,50 @@
private static final int DOT_STATE = 3;
private static final int MIN_STATE = 4;
private static final int MAX_STATE = 5;
- static final int FULL_LOCATION_CONVERTER = 1000;
- static final int METHOD_LOCATION_CONVERTER = 1001;
- static final int CLASS_LOCATION_CONVERTER = 1002;
- static final int LINE_LOCATION_CONVERTER = 1003;
- static final int FILE_LOCATION_CONVERTER = 1004;
- static final int RELATIVE_TIME_CONVERTER = 2000;
- static final int THREAD_CONVERTER = 2001;
- static final int LEVEL_CONVERTER = 2002;
- static final int NDC_CONVERTER = 2003;
- static final int MESSAGE_CONVERTER = 2004;
-
+
static HashMap globalRulesRegistry;
static {
// We set the global rules in the static initializer of PatternParser class
globalRulesRegistry = new HashMap(17);
globalRulesRegistry.put("c", LoggerPatternConverter.class.getName());
+ globalRulesRegistry.put("logger", LoggerPatternConverter.class.getName());
+
globalRulesRegistry.put("C", ClassNamePatternConverter.class.getName());
+ globalRulesRegistry.put("class", ClassNamePatternConverter.class.getName());
+
globalRulesRegistry.put("F", FileLocationPatternConverter.class.getName());
+ globalRulesRegistry.put("file", FileLocationPatternConverter.class.getName());
+
globalRulesRegistry.put("l", FullLocationPatternConverter.class.getName());
+
globalRulesRegistry.put("L", LineLocationPatternConverter.class.getName());
+ globalRulesRegistry.put("line", LineLocationPatternConverter.class.getName());
+
globalRulesRegistry.put("m", MessagePatternConverter.class.getName());
+ globalRulesRegistry.put("message", MessagePatternConverter.class.getName());
+
globalRulesRegistry.put("n", LineSeparatorPatternConverter.class.getName());
+
globalRulesRegistry.put(
"M", MethodLocationPatternConverter.class.getName());
+ globalRulesRegistry.put(
+ "method", MethodLocationPatternConverter.class.getName());
+
globalRulesRegistry.put("p", LevelPatternConverter.class.getName());
- globalRulesRegistry.put("level", FullLocationPatternConverter.class.getName());
+ globalRulesRegistry.put("level", LevelPatternConverter.class.getName());
globalRulesRegistry.put("r", RelativeTimePatternConverter.class.getName());
+ globalRulesRegistry.put("relative", RelativeTimePatternConverter.class.getName());
+
globalRulesRegistry.put("t", ThreadPatternConverter.class.getName());
+ globalRulesRegistry.put("thread", ThreadPatternConverter.class.getName());
+
globalRulesRegistry.put("x", NDCPatternConverter.class.getName());
+ globalRulesRegistry.put("ndc", NDCPatternConverter.class.getName());
+
globalRulesRegistry.put("X", MDCPatternConverter.class.getName());
+ globalRulesRegistry.put("mdc", MDCPatternConverter.class.getName());
}
int state;
@@ -161,28 +172,25 @@
*/
protected String extractConverter(char lastChar) {
+ // When this method is called, lastChar points to the first character of the
+ // conersion word. For example:
+ // For "%hello" lastChar = 'h'
+ // For "%-5hello" lastChar = 'h'
+
//System.out.println("lastchar is "+lastChar);
-
+
+ if(!Character.isUnicodeIdentifierStart(lastChar)) {
+ return null;
+ }
+
StringBuffer convBuf = new StringBuffer(16);
convBuf.append(lastChar);
- if (i >= patternLength) {
- // the pattern converter was the last character...
- return convBuf.toString();
- }
-
- char c = pattern.charAt(i);
-
- if (Character.isUnicodeIdentifierStart(c)) {
- convBuf.append(c);
- } else {
- return convBuf.toString();
- }
-
- while (
- (++i < patternLength)
- && Character.isUnicodeIdentifierPart(pattern.charAt(i))) {
+ while ((i < patternLength)
+ && Character.isUnicodeIdentifierPart(pattern.charAt(i))) {
convBuf.append(pattern.charAt(i));
+ //System.out.println("conv buffer is now ["+convBuf+"].");
+ i++;
}
return convBuf.toString();
@@ -359,18 +367,17 @@
protected void finalizeConverter(char c) {
PatternConverter pc = null;
- //System.out.println("============================");
String converterId = extractConverter(c);
- //System.out.println("==============[" + converterId + "]");
- //System.out.println("c is [" + c + "]");
+ System.out.println("converter ID[" + converterId + "]");
+ System.out.println("c is [" + c + "]");
String className = (String) findConverterClass(converterId);
- //System.out.println("==============[" + className + "]");
+ System.out.println("converter class [" + className + "]");
String option = extractOption();
- //System.out.println("Option is [" + option + "]");
+ System.out.println("Option is [" + option + "]");
if (className != null) {
pc =
(PatternConverter) OptionConverter.instantiateByClassName(
1.5 +1 -1 jakarta-log4j/tests/input/patternLayout14.properties
Index: patternLayout14.properties
===================================================================
RCS file: /home/cvs/jakarta-log4j/tests/input/patternLayout14.properties,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- patternLayout14.properties 18 Sep 2003 21:26:55 -0000 1.4
+++ patternLayout14.properties 19 Sep 2003 19:54:12 -0000 1.5
@@ -3,4 +3,4 @@
log4j.appender.testAppender.File= output/temp
log4j.appender.testAppender.Append=false
log4j.appender.testAppender.layout=pattern.MyPatternLayout
-log4j.appender.testAppender.layout.ConversionPattern=%5p %-4# - %m%n
+log4j.appender.testAppender.layout.ConversionPattern=%5p %-4counter - %m%n
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-dev-help@jakarta.apache.org
|