You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by ps...@apache.org on 2003/04/30 06:19:37 UTC
cvs commit: jakarta-log4j-sandbox/src/java/org/apache/log4j/xml XMLDecoder.java
psmith 2003/04/29 21:19:36
Modified: src/java/org/apache/log4j/xml XMLDecoder.java
Log:
reorded the way the Document is decoded into LoggingEvents to allow
for more than one event to be decoded. Added file decoding option.
Revision Changes Path
1.2 +169 -92 jakarta-log4j-sandbox/src/java/org/apache/log4j/xml/XMLDecoder.java
Index: XMLDecoder.java
===================================================================
RCS file: /home/cvs/jakarta-log4j-sandbox/src/java/org/apache/log4j/xml/XMLDecoder.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- XMLDecoder.java 28 Apr 2003 05:44:33 -0000 1.1
+++ XMLDecoder.java 30 Apr 2003 04:19:36 -0000 1.2
@@ -49,24 +49,26 @@
package org.apache.log4j.xml;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.LineNumberReader;
+import java.io.StringReader;
+import java.util.Hashtable;
+import java.util.Vector;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggingEvent;
-
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
-
import org.xml.sax.InputSource;
-import java.io.StringReader;
-
-import java.util.Hashtable;
-
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-
/**
* Decodes Logging Events in XML formated into elements that are used by
@@ -76,6 +78,7 @@
* even though the DTD supports multiple events nested in an eventSet.
*
* @author Scott Deboy <sd...@apache.org>
+ * @author Paul Smith <ps...@apache.org>
*
*/
public class XMLDecoder {
@@ -117,7 +120,15 @@
// complain. Indeed, "log4j.dtd" alone is not a valid URI which
// causes Crimson to barf. The Log4jEntityResolver only cares
// about the "log4j.dtd" ending.
- buf.setLength(0);
+
+
+// buf.setLength(0);
+/**
+ * resetting the length of the StringBuffer is dangerous, particularly
+ * on some JDK 1.4 impls, there's a known Bug that causes a memory leak
+ */
+ buf = new StringBuffer(1024);
+
buf.append(beginPart);
buf.append(data);
buf.append(endPart);
@@ -133,12 +144,56 @@
return document;
}
+/**
+ * Reads the contents of the file into a String
+ * @param file the file to load
+ * @return The contents of the file as a String
+ * @throws IOException if an error occurred during the loading process
+ */
+ private String loadFileSource(File file) throws IOException {
+
+ LineNumberReader reader = null;
+ StringBuffer buf = new StringBuffer(1024);
+ try {
+ reader = new LineNumberReader(new FileReader(file));
+ String line = null;
+ while ( (line = reader.readLine()) != null) {
+ buf.append(line);
+ }
+ } catch (IOException e) {
+ throw e;
+ }
+ finally {
+ try {
+ if (reader != null) {
+ reader.close();
+ }
+ } catch (Exception e) {
+
+ }
+ }
+ return buf.toString();
+ }
+
+ /**
+ * Decodes a File into a Vector of LoggingEvents
+ * @param file the file to decode events from
+ * @return Vector of LoggingEvents
+ * @throws IOException
+ */
+ public Vector decode(File file) throws IOException {
+
+ String fileContents = loadFileSource(file);
+ Document doc = parse(fileContents);
+ return decodeEvents(doc);
+ }
+
/**
* Converts the string data into an XML Document, and then soaks out the
* relevant bits to form a new LoggingEvent instance which can be used
* by any Log4j element locally.
* @param data
- * @return
+ * @return a single LoggingEvent
*/
public LoggingEvent decode(String data) {
Document document = parse(data);
@@ -146,7 +201,22 @@
if (document == null) {
return null;
}
+ Vector events = decodeEvents(document);
+ if (events.size() > 0) {
+ return (LoggingEvent)events.firstElement();
+ }
+ return null;
+ }
+
+ /**
+ * Given a Document, converts the XML into a Vector of LoggingEvents
+ * @param document
+ * @return
+ */
+ private Vector decodeEvents(Document document) {
+ Vector events = new Vector();
+
Logger logger = null;
long timeStamp = 0L;
Level level = null;
@@ -163,92 +233,99 @@
NodeList nl = document.getElementsByTagName("log4j:eventSet");
Node eventSet = nl.item(0);
- Node eventNode = eventSet.getFirstChild();
- logger =
- Logger.getLogger(
- eventNode.getAttributes().getNamedItem("logger").getNodeValue());
- timeStamp =
- Long.parseLong(
- eventNode.getAttributes().getNamedItem("timestamp").getNodeValue());
- level =
- Level.toLevel(
- eventNode.getAttributes().getNamedItem("level").getNodeValue());
- threadName =
- eventNode.getAttributes().getNamedItem("thread").getNodeValue();
-
- NodeList list = eventNode.getChildNodes();
- int listLength = list.getLength();
-
- for (int y = 0; y < listLength; y++) {
- String tagName = list.item(y).getNodeName();
-
- if (tagName.equalsIgnoreCase("log4j:message")) {
- message = getCData(list.item(y));
- }
-
- if (tagName.equalsIgnoreCase("log4j:NDC")) {
- ndc = getCData(list.item(y));
- }
-
- if (tagName.equalsIgnoreCase("log4j:MDC")) {
- mdc = new Hashtable();
-
- NodeList propertyList = list.item(y).getChildNodes();
- int propertyLength = propertyList.getLength();
-
- for (int i = 0; i < propertyLength; i++) {
- String propertyTag = propertyList.item(i).getNodeName();
-
- if (propertyTag.equalsIgnoreCase("log4j:data")) {
- Node property = propertyList.item(i);
- String name =
- property.getAttributes().getNamedItem("name").getNodeValue();
- String value =
- property.getAttributes().getNamedItem("value").getNodeValue();
- mdc.put(name, value);
+
+ NodeList eventList = eventSet.getChildNodes();
+ for (int eventIndex = 0; eventIndex < eventList.getLength(); eventIndex++) {
+ Node eventNode = eventList.item(eventIndex);
+
+ logger =
+ Logger.getLogger(
+ eventNode.getAttributes().getNamedItem("logger").getNodeValue());
+ timeStamp =
+ Long.parseLong(
+ eventNode.getAttributes().getNamedItem("timestamp").getNodeValue());
+ level =
+ Level.toLevel(
+ eventNode.getAttributes().getNamedItem("level").getNodeValue());
+ threadName =
+ eventNode.getAttributes().getNamedItem("thread").getNodeValue();
+
+ NodeList list = eventNode.getChildNodes();
+ int listLength = list.getLength();
+
+ for (int y = 0; y < listLength; y++) {
+ String tagName = list.item(y).getNodeName();
+
+ if (tagName.equalsIgnoreCase("log4j:message")) {
+ message = getCData(list.item(y));
+ }
+
+ if (tagName.equalsIgnoreCase("log4j:NDC")) {
+ ndc = getCData(list.item(y));
+ }
+
+ if (tagName.equalsIgnoreCase("log4j:MDC")) {
+ mdc = new Hashtable();
+
+ NodeList propertyList = list.item(y).getChildNodes();
+ int propertyLength = propertyList.getLength();
+
+ for (int i = 0; i < propertyLength; i++) {
+ String propertyTag = propertyList.item(i).getNodeName();
+
+ if (propertyTag.equalsIgnoreCase("log4j:data")) {
+ Node property = propertyList.item(i);
+ String name =
+ property.getAttributes().getNamedItem("name").getNodeValue();
+ String value =
+ property.getAttributes().getNamedItem("value").getNodeValue();
+ mdc.put(name, value);
+ }
}
}
- }
-
- if (tagName.equalsIgnoreCase("log4j:throwable")) {
- exception = new Throwable(getCData(list.item(y)));
- }
-
- if (tagName.equalsIgnoreCase("log4j:locationinfo")) {
- className =
- list.item(y).getAttributes().getNamedItem("class").getNodeValue();
- methodName =
- list.item(y).getAttributes().getNamedItem("method").getNodeValue();
- fileName =
- list.item(y).getAttributes().getNamedItem("file").getNodeValue();
- lineNumber =
- list.item(y).getAttributes().getNamedItem("line").getNodeValue();
- }
-
- if (tagName.equalsIgnoreCase("log4j:properties")) {
- properties = new Hashtable();
-
- NodeList propertyList = list.item(y).getChildNodes();
- int propertyLength = propertyList.getLength();
-
- for (int i = 0; i < propertyLength; i++) {
- String propertyTag = propertyList.item(i).getNodeName();
-
- if (propertyTag.equalsIgnoreCase("log4j:data")) {
- Node property = propertyList.item(i);
- String name =
- property.getAttributes().getNamedItem("name").getNodeValue();
- String value =
- property.getAttributes().getNamedItem("value").getNodeValue();
- properties.put(name, value);
+
+ if (tagName.equalsIgnoreCase("log4j:throwable")) {
+ exception = new Throwable(getCData(list.item(y)));
+ }
+
+ if (tagName.equalsIgnoreCase("log4j:locationinfo")) {
+ className =
+ list.item(y).getAttributes().getNamedItem("class").getNodeValue();
+ methodName =
+ list.item(y).getAttributes().getNamedItem("method").getNodeValue();
+ fileName =
+ list.item(y).getAttributes().getNamedItem("file").getNodeValue();
+ lineNumber =
+ list.item(y).getAttributes().getNamedItem("line").getNodeValue();
+ }
+
+ if (tagName.equalsIgnoreCase("log4j:properties")) {
+ properties = new Hashtable();
+
+ NodeList propertyList = list.item(y).getChildNodes();
+ int propertyLength = propertyList.getLength();
+
+ for (int i = 0; i < propertyLength; i++) {
+ String propertyTag = propertyList.item(i).getNodeName();
+
+ if (propertyTag.equalsIgnoreCase("log4j:data")) {
+ Node property = propertyList.item(i);
+ String name =
+ property.getAttributes().getNamedItem("name").getNodeValue();
+ String value =
+ property.getAttributes().getNamedItem("value").getNodeValue();
+ properties.put(name, value);
+ }
}
}
}
+
+ events.add(new LoggingEvent(
+ logger.getName(), logger, timeStamp, level, threadName, message, ndc, mdc,
+ exception, className, methodName, fileName, lineNumber, properties));
}
-
- return new LoggingEvent(
- logger.getName(), logger, timeStamp, level, threadName, message, ndc, mdc,
- exception, className, methodName, fileName, lineNumber, properties);
+
+ return events;
}
private String getCData(Node n) {
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-dev-help@jakarta.apache.org