ceki 2004/05/15 13:41:06
Modified: src/java/org/apache/joran ExecutionContext.java
src/java/org/apache/log4j/joran/action PropertyAction.java
src/java/org/apache/log4j/db DBReceiver.java
src/java/org/apache/log4j/spi LoggingEvent.java
tests/src/java/org/apache/log4j/db FullCycleDBTest.java
DBReeceiverTest.java
src/java/org/apache/log4j/joran JoranConfigurator.java
src/java/org/apache/joran/action Action.java
Added: tests/input/db writeCS1.xml readCS1.xml db.properties
Log:
Added FullCycleDBTest which tests both writing and reading through
DBAppender and DBReceiver.
- Fixed a bug in DBReceiver.
- Minor additions and fixes
Revision Changes Path
1.8 +11 -0 logging-log4j/src/java/org/apache/joran/ExecutionContext.java
Index: ExecutionContext.java
===================================================================
RCS file: /home/cvs/logging-log4j/src/java/org/apache/joran/ExecutionContext.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- ExecutionContext.java 15 May 2004 18:22:57 -0000 1.7
+++ ExecutionContext.java 15 May 2004 20:41:05 -0000 1.8
@@ -51,6 +51,17 @@
substitutionProperties = new Properties();
}
+// /**
+// * Clear the internal structures for reuse of the execution context
+// *
+// */
+// public void clear() {
+// objectStack.clear();
+// objectMap.clear();
+// errorList.clear();
+// substitutionProperties.clear();
+// }
+
public void addError(ErrorItem errorItem) {
Locator locator = joranInterpreter.getLocator();
1.2 +2 -1
logging-log4j/src/java/org/apache/log4j/joran/action/PropertyAction.java
Index: PropertyAction.java
===================================================================
RCS file:
/home/cvs/logging-log4j/src/java/org/apache/log4j/joran/action/PropertyAction.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- PropertyAction.java 15 May 2004 18:22:57 -0000 1.1
+++ PropertyAction.java 15 May 2004 20:41:05 -0000 1.2
@@ -42,7 +42,7 @@
public class PropertyAction extends Action {
static final Logger logger = Logger.getLogger(PropertyAction.class);
static String INVALID_ATTRIBUTES =
- "In <property> element, either the filename attribute or both the name and
value attributes must be set.";
+ "In <property> element, either the \"file\" attribute or both the \"name\" and
\"value\" attributes must be set.";
/**
* Set a new property for the execution context by name, value pair, or adds
@@ -64,6 +64,7 @@
FileInputStream istream = new FileInputStream(fileName);
props.load(istream);
istream.close();
+ ec.addProperties(props);
} catch (IOException e) {
String errMsg = "Could not read properties file [" + fileName + "].";
LogLog.error(errMsg, e);
1.3 +2 -2 logging-log4j/src/java/org/apache/log4j/db/DBReceiver.java
Index: DBReceiver.java
===================================================================
RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/db/DBReceiver.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DBReceiver.java 12 May 2004 13:21:41 -0000 1.2
+++ DBReceiver.java 15 May 2004 20:41:05 -0000 1.3
@@ -127,7 +127,7 @@
*/
class DBReceiverJob implements Job {
- long lastId = 0;
+ long lastId = 1;
public void execute() {
@@ -146,7 +146,7 @@
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(sql.toString());
- rs.first();
+ rs.beforeFirst();
while (rs.next()) {
LoggingEvent event = new LoggingEvent();
1.1 logging-log4j/tests/input/db/writeCS1.xml
Index: writeCS1.xml
===================================================================
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration>
<log4j:configuration debug="true">
<property file="input/db/db.properties"/>
<appender name="DB" class="org.apache.log4j.db.DBAppender">
<connectionSource class="org.apache.log4j.db.UrlConnectionSource">
<param name="driverClass" value="${driverClass}"/>
<param name="url" value="${url}"/>
<param name="user" value="${user}"/>
<param name="password" value="${password}"/>
</connectionSource>
</appender>
<root>
<level value ="debug"/>
<appender-ref ref="DB" />
</root>
</log4j:configuration>
1.1 logging-log4j/tests/input/db/readCS1.xml
Index: readCS1.xml
===================================================================
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration>
<log4j:configuration debug="true">
<property file="input/db/db.properties"/>
<plugin name="DB1" class="org.apache.log4j.db.DBReceiver">
<connectionSource class="org.apache.log4j.db.UrlConnectionSource">
<param name="driverClass" value="${driverClass}"/>
<param name="url" value="${url}"/>
<param name="user" value="${user}"/>
<param name="password" value="${password}"/>
</connectionSource>
</plugin>
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="ZZZ %relative %level %logger -
%message%n"/>
</layout>
</appender>
<appender name="VECTOR" class="org.apache.log4j.VectorAppender">
</appender>
<root>
<level value="debug"/>
<appender-ref ref="VECTOR" />
<appender-ref ref="CONSOLE" />
</root>
</log4j:configuration>
1.1 logging-log4j/tests/input/db/db.properties
Index: db.properties
===================================================================
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql:///test
user=root
password=
1.62 +7 -0 logging-log4j/src/java/org/apache/log4j/spi/LoggingEvent.java
Index: LoggingEvent.java
===================================================================
RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/spi/LoggingEvent.java,v
retrieving revision 1.61
retrieving revision 1.62
diff -u -r1.61 -r1.62
--- LoggingEvent.java 14 May 2004 06:20:13 -0000 1.61
+++ LoggingEvent.java 15 May 2004 20:41:05 -0000 1.62
@@ -201,6 +201,13 @@
private LocationInfo locationInfo;
/**
+ * @return The current sequenceCount for this JVM.
+ */
+
+ public static long getSequenceCount() {
+ return sequenceCount;
+ }
+ /**
* The no-argument constructor for LoggingEvent. This method is the recommended
* constructor for creating LoggingEvent instances.
*
1.2 +64 -62
logging-log4j/tests/src/java/org/apache/log4j/db/FullCycleDBTest.java
Index: FullCycleDBTest.java
===================================================================
RCS file:
/home/cvs/logging-log4j/tests/src/java/org/apache/log4j/db/FullCycleDBTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FullCycleDBTest.java 15 May 2004 18:22:57 -0000 1.1
+++ FullCycleDBTest.java 15 May 2004 20:41:05 -0000 1.2
@@ -15,26 +15,27 @@
*/
package org.apache.log4j.db;
-import java.io.BufferedReader;
import java.io.FileInputStream;
-import java.io.InputStreamReader;
import java.util.Hashtable;
+import java.util.Iterator;
import java.util.Properties;
+import java.util.Vector;
import javax.naming.Context;
import javax.naming.InitialContext;
import junit.framework.TestCase;
-import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Hierarchy;
import org.apache.log4j.LogManager;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
+import org.apache.log4j.VectorAppender;
import org.apache.log4j.helpers.IntializationUtil;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.joran.JoranConfigurator;
import org.apache.log4j.spi.LoggerRepository;
+import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.RootLogger;
@@ -69,75 +70,76 @@
super(arg0);
}
- public void testBasic()
+
+ /**
+ * This test starts by writing a single element to a DB using DBAppender
+ * and then reads it back using DBReceiver.
+ *
+ * @throws Exception
+ */
+ public void testSingleOutput()
throws Exception {
-
- BasicConfigurator.configure();
- LogLog.setInternalDebugging(true);
LoggerRepository lrWrite = new Hierarchy(new RootLogger(Level.DEBUG));
-
IntializationUtil.log4jInternalConfiguration(lrWrite);
+ JoranConfigurator jc1 = new JoranConfigurator();
+ jc1.doConfigure("input/db/writeCS1.xml", lrWrite);
+ long startTime = System.currentTimeMillis();
+ //LogLog.info("***start time is "+startTime);
- LoggerRepository lrRead = new Hierarchy(new RootLogger(Level.DEBUG));
-
- Properties props = new Properties();
- FileInputStream istream = new FileInputStream("input/db/db.properties");
- props.load(istream);
- istream.close();
- JoranConfigurator jc = new JoranConfigurator();
- jc.getExecutionContext().addProperties(props);
- jc.doConfigure("input/db/writeCS1.xml", lrWrite);
-
-
- Logger out = lrWrite.getLogger("test");
+ // Write out just one log message
+ Logger out = lrWrite.getLogger("testSingleOutput");
+ long sn = LoggingEvent.getSequenceCount();
out.debug("test1");
-
-
-
-// UrlConnectionSource connectionSource = new UrlConnectionSource();
-// connectionSource.setDriverClass("com.mysql.jdbc.Driver");
-// connectionSource.setUrl("jdbc:mysql:///test");
-// connectionSource.setUser("root");
-// LogLog.info("xxxxxxx");
-//
-// DBReceiver dbReceiver = new DBReceiver();
-// dbReceiver.setLoggerRepository(LogManager.getLoggerRepository());
-// dbReceiver.setConnectionSource(connectionSource);
-// dbReceiver.activateOptions();
-// LogLog.info("after dbReceiver.activateOptions()");
-
-// try {
-// Thread.sleep(3000);
-// } catch (Exception e) {
-// }
- //dbReceiver.shutdown();
- LogLog.info("after dbReceiver.shutdown()");
-
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
-
- while (true) {
- System.out.println("Type a message to send to log server. Type 'q' to quit.");
- String s = reader.readLine();
- if (s.equals("q")) {
- break;
- } else {
- }
+
+ // now read it back
+ LoggerRepository lrRead = new Hierarchy(new RootLogger(Level.DEBUG));
+ IntializationUtil.log4jInternalConfiguration(lrRead);
+ JoranConfigurator jc2 = new JoranConfigurator();
+ jc2.doConfigure("input/db/readCS1.xml", lrRead);
+
+ VectorAppender va = (VectorAppender)
lrRead.getRootLogger().getAppender("VECTOR");
+ try { Thread.sleep(1100); } catch(Exception e) {}
+ Vector v = getCleanedVector(va, startTime);
+ assertEquals(1, v.size());
+ LoggingEvent eventBack = (LoggingEvent) v.get(0);
+ assertEquals("testSingleOutput", eventBack.getLoggerName());
+ if(eventBack.getTimeStamp() < startTime) {
+ fail("Returned event cannot preceed start of the test");
}
+ assertEquals("testSingleOutput", eventBack.getLoggerName());
+ assertEquals(sn, eventBack.getSequenceNumber());
LogManager.getLoggerRepository().shutdown();
}
- public void xtestJNDI()
- throws Exception {
- Hashtable env = new Hashtable();
-
- env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
- env.put(Context.PROVIDER_URL, "file:///home/jndi");
-
- Context ctx = new InitialContext(env);
-
- //ctx.addToEnvironment("toto", new Integer(1));
- ctx.bind("toto", new Integer(1));
+ Vector getCleanedVector(VectorAppender va, long startTime) {
+ assertNotNull(va);
+ Vector v = va.getVector();
+ // remove all elements older than startTime
+ for(Iterator i = v.iterator(); i.hasNext(); ) {
+ LoggingEvent event = (LoggingEvent) i.next();
+ if(startTime > event.getTimeStamp()) {
+ LogLog.info("***Removing event with timestamp "+event.getTimeStamp());
+ i.remove();
+ } else {
+ LogLog.info("***Keeping event with timestamo"+event.getTimeStamp());
+ }
+ }
+ return v;
}
+
+
+// public void xtestJNDI()
+// throws Exception {
+// Hashtable env = new Hashtable();
+//
+// env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
+// env.put(Context.PROVIDER_URL, "file:///home/jndi");
+//
+// Context ctx = new InitialContext(env);
+//
+// //ctx.addToEnvironment("toto", new Integer(1));
+// ctx.bind("toto", new Integer(1));
+// }
}
1.4 +0 -1
logging-log4j/tests/src/java/org/apache/log4j/db/DBReeceiverTest.java
Index: DBReeceiverTest.java
===================================================================
RCS file:
/home/cvs/logging-log4j/tests/src/java/org/apache/log4j/db/DBReeceiverTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- DBReeceiverTest.java 12 May 2004 20:16:18 -0000 1.3
+++ DBReeceiverTest.java 15 May 2004 20:41:05 -0000 1.4
@@ -24,7 +24,6 @@
import junit.framework.TestCase;
-import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.LogManager;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.joran.JoranConfigurator;
1.8 +8 -9
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.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- JoranConfigurator.java 15 May 2004 18:22:57 -0000 1.7
+++ JoranConfigurator.java 15 May 2004 20:41:05 -0000 1.8
@@ -44,6 +44,7 @@
import org.apache.log4j.joran.action.LevelAction;
import org.apache.log4j.joran.action.LoggerAction;
import org.apache.log4j.joran.action.PluginAction;
+import org.apache.log4j.joran.action.PropertyAction;
import org.apache.log4j.joran.action.RootLoggerAction;
import org.apache.log4j.spi.Configurator;
import org.apache.log4j.spi.LoggerRepository;
@@ -51,10 +52,11 @@
/**
- * @author ceki
- *
- * To change the template for this generated type comment go to
- * Window>Preferences>Java>Code Generation>Code and Comments
+ *
+ * A JoranConfigurator instance should not be used more than once to
+ * configure a LoggerRepository.
+ *
+ * @author Ceki Gülcü
*/
public class JoranConfigurator
implements Configurator {
@@ -66,10 +68,7 @@
public void doConfigure(URL url, LoggerRepository repository) {
ExecutionContext ec = joranInterpreter.getExecutionContext();
- ec.pushObject(repository);
-
String errMsg;
-
try {
InputStream in = url.openStream();
doConfigure(in, repository);
@@ -88,7 +87,6 @@
public void doConfigure(String filename, LoggerRepository repository) {
FileInputStream fis = null;
ExecutionContext ec = joranInterpreter.getExecutionContext();
- ec.pushObject(repository);
LogLog.info("in JoranConfigurator doConfigure "+filename);
try {
fis = new FileInputStream(filename);
@@ -112,6 +110,8 @@
protected void selfInitialize() {
RuleStore rs = new SimpleRuleStore();
rs.addRule(new Pattern("log4j:configuration"), new ConfigurationAction());
+ rs.addRule(new Pattern("log4j:configuration/property"), new PropertyAction());
+ rs.addRule(new Pattern("log4j:configuration/plugin"), new PluginAction());
rs.addRule(new Pattern("log4j:configuration/logger"), new LoggerAction());
rs.addRule(new Pattern("log4j:configuration/logger/level"), new LevelAction());
rs.addRule(new Pattern("log4j:configuration/root"), new RootLoggerAction());
@@ -121,7 +121,6 @@
rs.addRule(new Pattern("log4j:configuration/appender"), new AppenderAction());
rs.addRule(new Pattern("log4j:configuration/appender/layout"), new
LayoutAction());
rs.addRule(new Pattern("log4j:configuration/appender/layout/conversionRule"),
new ConversionRuleAction());
- rs.addRule(new Pattern("log4j:configuration/plugin"), new PluginAction());
rs.addRule(new Pattern("log4j:configuration/newRule"), new NewRuleAction());
rs.addRule(new Pattern("*/param"), new ParamAction());
1.14 +1 -1 logging-log4j/src/java/org/apache/joran/action/Action.java
Index: Action.java
===================================================================
RCS file: /home/cvs/logging-log4j/src/java/org/apache/joran/action/Action.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- Action.java 15 May 2004 18:22:56 -0000 1.13
+++ Action.java 15 May 2004 20:41:05 -0000 1.14
@@ -39,7 +39,7 @@
public abstract class Action {
public static final String NAME_ATTRIBUTE = "name";
public static final String VALUE_ATTRIBUTE = "value";
- public static final String FILE_ATTRIBUTE = "value";
+ public static final String FILE_ATTRIBUTE = "file";
public static final String CLASS_ATTRIBUTE = "class";
public static final String PATTERN_ATTRIBUTE = "pattern";
public static final String ACTION_CLASS_ATTRIBUTE = "actionClass";
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]