in src/main/java/org/apache/log4j/xml/XMLDecoder.java [306:470]
private Vector<ChainsawLoggingEvent> decodeEvents(final Document document) {
Vector<ChainsawLoggingEvent> events = new Vector<>();
String logger;
long timeStamp;
String level;
String threadName;
String message = null;
String ndc = null;
String[] exception = null;
String className = null;
String methodName = null;
String fileName = null;
String lineNumber = null;
Hashtable properties = null;
NodeList nl = document.getElementsByTagName("log4j:eventSet");
Node eventSet = nl.item(0);
NodeList eventList = eventSet.getChildNodes();
for (int eventIndex = 0; eventIndex < eventList.getLength();
eventIndex++) {
Node eventNode = eventList.item(eventIndex);
//ignore carriage returns in xml
if (eventNode.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
logger = eventNode.getAttributes().getNamedItem("logger").getNodeValue();
timeStamp = Long.parseLong(eventNode.getAttributes().getNamedItem("timestamp").getNodeValue());
level = eventNode.getAttributes().getNamedItem("level").getNodeValue();
threadName = eventNode.getAttributes().getNamedItem("thread").getNodeValue();
NodeList list = eventNode.getChildNodes();
int listLength = list.getLength();
if (listLength == 0) {
continue;
}
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));
}
//still support receiving of MDC and convert to properties
if (tagName.equalsIgnoreCase("log4j:MDC")) {
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")) {
String exceptionString = getCData(list.item(y));
if (exceptionString != null && !exceptionString.trim().equals("")) {
exception = new String[]{exceptionString.trim()
};
}
}
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")) {
if (properties == null) {
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);
}
}
}
/**
* We add all the additional properties to the properties
* hashtable. Override properties that already exist
*/
if (additionalProperties.size() > 0) {
if (properties == null) {
properties = new Hashtable(additionalProperties);
}
for (Object o : additionalProperties.entrySet()) {
Map.Entry e = (Map.Entry) o;
properties.put(e.getKey(), e.getValue());
}
}
}
LocationInfo info;
if ((fileName != null)
|| (className != null)
|| (methodName != null)
|| (lineNumber != null)) {
info = new LocationInfo(fileName, className, methodName,
Integer.parseInt(lineNumber));
} else {
info = null;
}
// ThrowableInformation throwableInfo = null;
// if (exception != null) {
// throwableInfo = new ThrowableInformation(exception);
// }
builder.clear();
builder.setLogger(logger)
.setTimestamp(Instant.ofEpochMilli(timeStamp))
.setLevelFromString(level)
.setMessage(message)
.setThreadName(threadName)
.setMDC(properties)
.setNDC(ndc)
.setLocationInfo(info);
events.add(builder.create());
message = null;
ndc = null;
exception = null;
className = null;
methodName = null;
fileName = null;
lineNumber = null;
properties = null;
}
return events;
}