ceki 2004/11/22 09:07:09
Modified: tests/src/java/org/apache/log4j/rolling
TimeBasedRollingTest.java
src/java/org/apache/log4j/config ConfiguratorBase.java
src/java/org/apache/log4j/rolling
TimeBasedRollingPolicy.java
src/java/org/apache/log4j/joran JoranConfigurator.java
src/java/org/apache/log4j/joran/action
ConfigurationAction.java
Added: src/java/org/apache/log4j/joran/util XMLUtil.java
WellfomednessChecker.java
Log:
Tring to add a well-formedness check in JoranConfigurator
Revision Changes Path
1.9 +56 -19
logging-log4j/tests/src/java/org/apache/log4j/rolling/TimeBasedRollingTest.java
Index: TimeBasedRollingTest.java
===================================================================
RCS file:
/home/cvs/logging-log4j/tests/src/java/org/apache/log4j/rolling/TimeBasedRollingTest.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- TimeBasedRollingTest.java 20 Nov 2004 19:12:26 -0000 1.8
+++ TimeBasedRollingTest.java 22 Nov 2004 17:07:08 -0000 1.9
@@ -20,10 +20,10 @@
import junit.framework.TestCase;
import junit.framework.TestSuite;
-import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
+import org.apache.log4j.joran.JoranConfigurator;
import org.apache.log4j.util.Compare;
import java.text.SimpleDateFormat;
@@ -39,20 +39,19 @@
*/
public class TimeBasedRollingTest extends TestCase {
Logger logger = Logger.getLogger(TimeBasedRollingTest.class);
- Logger root;
public TimeBasedRollingTest(String name) {
super(name);
}
public void setUp() {
- root = Logger.getRootLogger();
- root.addAppender(
- new ConsoleAppender(new PatternLayout("%d{ABSOLUTE} %c{1} - %m%n")));
+ //root = Logger.getRootLogger();
+ //root.addAppender(
+ //new ConsoleAppender(new PatternLayout("%d{ABSOLUTE} [%t] %level %c -
%m%n")));
}
public void tearDown() {
- LogManager.shutdown();
+ //LogManager.shutdown();
}
/**
@@ -86,9 +85,9 @@
cal.add(Calendar.SECOND, 1);
}
- root.debug("Waiting until next second and 100 millis.");
+ System.out.println("Waiting until next second and 100 millis.");
delayUntilNextSecond(100);
- root.debug("Done waiting.");
+ System.out.println("Done waiting.");
for (int i = 0; i < 5; i++) {
logger.debug("Hello---" + i);
@@ -135,9 +134,9 @@
cal.add(Calendar.SECOND, 1);
}
- root.debug("Waiting until next second and 100 millis.");
+ System.out.println("Waiting until next second and 100 millis.");
delayUntilNextSecond(100);
- root.debug("Done waiting.");
+ System.out.println("Done waiting.");
for (int i = 0; i <= 2; i++) {
logger.debug("Hello---" + i);
@@ -201,9 +200,9 @@
filenames[3] = "output/test3-" + sdf.format(cal.getTime());
- root.debug("Waiting until next second and 100 millis.");
+ System.out.println("Waiting until next second and 100 millis.");
delayUntilNextSecond(100);
- root.debug("Done waiting.");
+ System.out.println("Done waiting.");
for (int i = 0; i < 5; i++) {
logger.debug("Hello---" + i);
@@ -265,9 +264,9 @@
filenames[3] = "output/test5.log";
- root.debug("Waiting until next second and 100 millis.");
+ System.out.println("Waiting until next second and 100 millis.");
delayUntilNextSecond(100);
- root.debug("Done waiting.");
+ System.out.println("Done waiting.");
for (int i = 0; i < 5; i++) {
logger.debug("Hello---" + i);
@@ -313,9 +312,9 @@
filenames[3] = "output/test6.log";
- root.debug("Waiting until next second and 100 millis.");
+ System.out.println("Waiting until next second and 100 millis.");
delayUntilNextSecond(100);
- root.debug("Done waiting.");
+ System.out.println("Done waiting.");
for (int i = 0; i < 5; i++) {
logger.debug("Hello---" + i);
@@ -333,6 +332,42 @@
assertTrue(Compare.compare(filenames[3], "witness/rolling/tbr-test6.3"));
}
+ public void testWithJoran1() throws Exception {
+ JoranConfigurator jc = new JoranConfigurator();
+ jc.doConfigure("./input/rolling/time1.xml",
LogManager.getLoggerRepository());
+ jc.dumpErrors();
+
+
+ String datePattern = "yyyy-MM-dd_HH_mm_ss";
+
+ SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
+ String[] filenames = new String[4];
+
+ Calendar cal = Calendar.getInstance();
+
+ for (int i = 0; i < 4; i++) {
+ filenames[i] = "output/test1-" + sdf.format(cal.getTime());
+ cal.add(Calendar.SECOND, 1);
+ }
+
+ System.out.println("Waiting until next second and 100 millis.");
+ delayUntilNextSecond(100);
+ System.out.println("Done waiting.");
+
+ for (int i = 0; i < 5; i++) {
+ logger.debug("Hello---" + i);
+ Thread.sleep(500);
+ }
+
+ for (int i = 0; i < 4; i++) {
+ //System.out.println(i + " expected filename [" + filenames[i] + "].");
+ }
+
+ for (int i = 0; i < 4; i++) {
+ assertTrue(Compare.compare(filenames[i], "witness/rolling/tbr-test1."
+ i));
+ }
+
+ }
void delayUntilNextSecond(int millis) {
long now = System.currentTimeMillis();
Calendar cal = Calendar.getInstance();
@@ -372,10 +407,12 @@
// suite.addTest(new TimeBasedRollingTest("test2"));
// suite.addTest(new TimeBasedRollingTest("test3"));
// suite.addTest(new TimeBasedRollingTest("test4"));
-
- suite.addTest(new TimeBasedRollingTest("test5"));
+//
+// suite.addTest(new TimeBasedRollingTest("test5"));
// suite.addTest(new TimeBasedRollingTest("test6"));
-
+ suite.addTest(new TimeBasedRollingTest("testWithJoran1"));
+
+
return suite;
}
}
1.4 +1 -1
logging-log4j/src/java/org/apache/log4j/config/ConfiguratorBase.java
Index: ConfiguratorBase.java
===================================================================
RCS file:
/home/cvs/logging-log4j/src/java/org/apache/log4j/config/ConfiguratorBase.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ConfiguratorBase.java 19 Nov 2004 15:20:06 -0000 1.3
+++ ConfiguratorBase.java 22 Nov 2004 17:07:08 -0000 1.4
@@ -106,7 +106,7 @@
ConsoleAppender appender = new ConsoleAppender();
appender.setLayout(
- new PatternLayout("LOG4J-INTERNAL: %d %level [%t] %c - %m%n"));
+ new PatternLayout("TEMPORARY CONSOLE APPPENDER: %d %level [%t] %c -
%m%n"));
appender.setName(Constants.TEMP_CONSOLE_APPENDER_NAME);
appender.activateOptions();
ll.addAppender(appender);
1.1
logging-log4j/src/java/org/apache/log4j/joran/util/XMLUtil.java
Index: XMLUtil.java
===================================================================
/*
* Created on Nov 22, 2004
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package org.apache.log4j.joran.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.log4j.spi.ErrorItem;
import org.xml.sax.InputSource;
/**
* @author ceki
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class XMLUtil {
public static final int CANT_SAY = 0;
public static final int WELL_FORMED = 1;
public static final int ILL_FORMED = 2;
public static final int UNRECOVERABLE_ERROR = 3;
public static int checkIfWellFormed(InputStream is, List errorList) {
int result;
if(is.markSupported()) {
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
try {
is.mark(64*1024*1024);
InputSource inputSource = new InputSource(is);
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setValidating(false);
SAXParser saxParser = spf.newSAXParser();
int oldSize = errorList.size();
saxParser.parse(inputSource, new WellfomednessChecker(errorList));
int newSize = errorList.size();
if(newSize > oldSize) {
result = ILL_FORMED;
} else {
result = WELL_FORMED;
}
} catch(Exception ex) {
errorList.add(new ErrorItem("Problem while hecking well-formedness",
ex));
result = ILL_FORMED;
} finally {
try {
is.reset();
} catch(IOException e) {
result = UNRECOVERABLE_ERROR;
}
}
} else {
System.out.println("XXXXXXXXXXXXX markSupported NOT supported");
return CANT_SAY;
}
return result;
}
}
1.1
logging-log4j/src/java/org/apache/log4j/joran/util/WellfomednessChecker.java
Index: WellfomednessChecker.java
===================================================================
/*
* Created on Nov 22, 2004
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package org.apache.log4j.joran.util;
import java.util.List;
import org.apache.log4j.spi.ErrorItem;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
/**
* As the name indicated this ContentHander is used to check the
* well-formedness of an XML document.
*
* @author Ceki Gulcu
*
*/
public class WellfomednessChecker extends DefaultHandler {
List errorList;
WellfomednessChecker(List errorList) {
this.errorList = errorList;
}
public void error(SAXParseException spe) throws SAXException {
ErrorItem errorItem = new ErrorItem("Parsing error", spe);
errorItem.setLineNumber(spe.getLineNumber());
errorItem.setColNumber(spe.getColumnNumber());
errorList.add(errorItem);
}
public void fatalError(SAXParseException spe) throws SAXException {
ErrorItem errorItem = new ErrorItem("Parsing fatal error", spe);
errorItem.setLineNumber(spe.getLineNumber());
errorItem.setColNumber(spe.getColumnNumber());
errorList.add(errorItem);
}
public void warning(SAXParseException spe) throws SAXException {
ErrorItem errorItem = new ErrorItem("Parsing warning", spe);
errorItem.setLineNumber(spe.getLineNumber());
errorItem.setColNumber(spe.getColumnNumber());
errorList.add(errorItem);
}
}
1.8 +1 -0
logging-log4j/src/java/org/apache/log4j/rolling/TimeBasedRollingPolicy.java
Index: TimeBasedRollingPolicy.java
===================================================================
RCS file:
/home/cvs/logging-log4j/src/java/org/apache/log4j/rolling/TimeBasedRollingPolicy.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- TimeBasedRollingPolicy.java 31 May 2004 21:29:15 -0000 1.7
+++ TimeBasedRollingPolicy.java 22 Nov 2004 17:07:08 -0000 1.8
@@ -63,6 +63,7 @@
fileNamePattern = new
FileNamePattern(fileNamePatternStr.substring(0, len -4));
compressionMode = Compress.GZ;
} else {
+ logger.debug("No compression will be used");
fileNamePattern = new FileNamePattern(fileNamePatternStr);
compressionMode = Compress.NONE;
}
1.20 +15 -3
logging-log4j/src/java/org/apache/log4j/joran/JoranConfigurator.java
Index: JoranConfigurator.java
===================================================================
RCS file:
/home/cvs/logging-log4j/src/java/org/apache/log4j/joran/JoranConfigurator.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- JoranConfigurator.java 19 Nov 2004 15:20:06 -0000 1.19
+++ JoranConfigurator.java 22 Nov 2004 17:07:08 -0000 1.20
@@ -34,7 +34,6 @@
import org.apache.joran.action.NewRuleAction;
import org.apache.joran.action.ParamAction;
import org.apache.joran.helper.SimpleRuleStore;
-//import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.config.ConfiguratorBase;
import org.apache.log4j.joran.action.ActionConst;
import org.apache.log4j.joran.action.AppenderAction;
@@ -49,6 +48,7 @@
import org.apache.log4j.joran.action.RepositoryPropertyAction;
import org.apache.log4j.joran.action.RootLoggerAction;
import org.apache.log4j.joran.action.SubstitutionPropertyAction;
+import org.apache.log4j.joran.util.XMLUtil;
import org.apache.log4j.spi.ErrorItem;
import org.apache.log4j.spi.LoggerRepository;
import org.apache.log4j.xml.Log4jEntityResolver;
@@ -126,7 +126,18 @@
* Configure a repository from the input stream passed as parameter
*/
public void doConfigure(InputStream in, LoggerRepository repository) {
- doConfigure(new InputSource(in), repository);
+ List errorList = getErrorList();
+
+ int result = XMLUtil.checkIfWellFormed(in, errorList);
+ switch(result) {
+ case XMLUtil.ILL_FORMED:
+ errorList.add(new ErrorItem("Ill formed XML document. Abandoning all
furhter processing."));
+ break;
+ case XMLUtil.CANT_SAY:
+ case XMLUtil.WELL_FORMED:
+ doConfigure(new InputSource(in), repository);
+ }
+
}
/**
@@ -140,7 +151,7 @@
try {
attachListAppender(repository);
- getLogger(repository).debug("Starting to parse configuration {}",
inputSource);
+ getLogger(repository).debug("Starting to parse input source.");
SAXParserFactory spf = SAXParserFactory.newInstance();
// we want non-validating parsers
spf.setValidating(false);
@@ -163,6 +174,7 @@
}
+
protected void selfInitialize() {
RuleStore rs = new SimpleRuleStore();
rs.addRule(new Pattern("log4j:configuration"), new
ConfigurationAction());
1.5 +1 -6
logging-log4j/src/java/org/apache/log4j/joran/action/ConfigurationAction.java
Index: ConfigurationAction.java
===================================================================
RCS file:
/home/cvs/logging-log4j/src/java/org/apache/log4j/joran/action/ConfigurationAction.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- ConfigurationAction.java 18 Nov 2004 11:47:52 -0000 1.4
+++ ConfigurationAction.java 22 Nov 2004 17:07:09 -0000 1.5
@@ -45,12 +45,7 @@
}
}
-
-
- public void finish(ExecutionContext ec) {
- }
-
- public void end(ExecutionContext ec, String e) {
+ public void end(ExecutionContext ec, String name) {
if (attachment) {
LoggerRepository repository = (LoggerRepository) ec.getObject(0);
List errorList = ec.getErrorList();
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]