@Test public void testDoLayout() throws Exception { ILoggingEvent le = createLoggingEvent(); String result = DOCTYPE + "<log4j:eventSet xmlns:log4j='http: if (layout.getFileHeader() != null) { result += layout.getFileHeader(); } if (layout.getPresentationHeader() != null) { result += layout.getPresentationHeader(); } result += layout.doLayout(le); if (layout.getPresentationFooter() != null) { result += layout.getPresentationFooter(); } if (layout.getFileFooter() != null) { result += layout.getFileFooter(); } result += "</log4j:eventSet>"; Document document = parse(result); XPath xpath = this.newXPath(); NodeList eventNodes = (NodeList) xpath.compile(" Assert.assertEquals(1, eventNodes.getLength()); Assert.assertEquals(MESSAGE, xpath.compile(" NodeList dataNodes = (NodeList) xpath.compile(" boolean foundMdc = false; for (int i = 0; i != dataNodes.getLength(); ++i) { Node dataNode = dataNodes.item(i); if (dataNode.getAttributes().getNamedItem("name").getNodeValue().equals(MDC_KEY)) { foundMdc = true; Assert.assertEquals(MDC_VALUE, dataNode.getAttributes().getNamedItem("value").getNodeValue()); break; } } Assert.assertTrue(foundMdc); }
public String doLayout(ILoggingEvent event) { if (buf.capacity() > UPPER_LIMIT) { buf = new StringBuilder(DEFAULT_SIZE); } else { buf.setLength(0); } buf.append("<log4j:event logger=\""); buf.append(Transform.escapeTags(event.getLoggerName())); buf.append("\"\r\n"); buf.append(" timestamp=\""); buf.append(event.getTimeStamp()); buf.append("\" level=\""); buf.append(event.getLevel()); buf.append("\" thread=\""); buf.append(Transform.escapeTags(event.getThreadName())); buf.append("\">\r\n"); buf.append(" <log4j:message>"); buf.append(Transform.escapeTags(event.getFormattedMessage())); buf.append("</log4j:message>\r\n"); IThrowableProxy tp = event.getThrowableProxy(); if (tp != null) { StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray(); buf.append(" <log4j:throwable><![CDATA["); for (StackTraceElementProxy step : stepArray) { buf.append(CoreConstants.TAB); buf.append(step.toString()); buf.append("\r\n"); } buf.append("]]></log4j:throwable>\r\n"); } if (locationInfo) { StackTraceElement[] callerDataArray = event.getCallerData(); if (callerDataArray != null && callerDataArray.length > 0) { StackTraceElement immediateCallerData = callerDataArray[0]; buf.append(" <log4j:locationInfo class=\""); buf.append(immediateCallerData.getClassName()); buf.append("\"\r\n"); buf.append(" method=\""); buf.append(Transform.escapeTags(immediateCallerData.getMethodName())); buf.append("\" file=\""); buf.append(Transform.escapeTags(immediateCallerData.getFileName())); buf.append("\" line=\""); buf.append(immediateCallerData.getLineNumber()); buf.append("\"/>\r\n"); } } if (this.getProperties()) { Map<String, String> propertyMap = event.getMDCPropertyMap(); if ((propertyMap != null) && (propertyMap.size() != 0)) { Set<Entry<String, String>> entrySet = propertyMap.entrySet(); buf.append(" <log4j:properties>"); for (Entry<String, String> entry : entrySet) { buf.append("\r\n <log4j:data"); buf.append(" name='" + Transform.escapeTags(entry.getKey()) + "'"); buf.append(" value='" + Transform.escapeTags(entry.getValue()) + "'"); buf.append(" />"); } buf.append("\r\n </log4j:properties>"); } } buf.append("\r\n</log4j:event>\r\n\r\n"); return buf.toString(); }
XMLLayout extends LayoutBase<ILoggingEvent> { public String doLayout(ILoggingEvent event) { if (buf.capacity() > UPPER_LIMIT) { buf = new StringBuilder(DEFAULT_SIZE); } else { buf.setLength(0); } buf.append("<log4j:event logger=\""); buf.append(Transform.escapeTags(event.getLoggerName())); buf.append("\"\r\n"); buf.append(" timestamp=\""); buf.append(event.getTimeStamp()); buf.append("\" level=\""); buf.append(event.getLevel()); buf.append("\" thread=\""); buf.append(Transform.escapeTags(event.getThreadName())); buf.append("\">\r\n"); buf.append(" <log4j:message>"); buf.append(Transform.escapeTags(event.getFormattedMessage())); buf.append("</log4j:message>\r\n"); IThrowableProxy tp = event.getThrowableProxy(); if (tp != null) { StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray(); buf.append(" <log4j:throwable><![CDATA["); for (StackTraceElementProxy step : stepArray) { buf.append(CoreConstants.TAB); buf.append(step.toString()); buf.append("\r\n"); } buf.append("]]></log4j:throwable>\r\n"); } if (locationInfo) { StackTraceElement[] callerDataArray = event.getCallerData(); if (callerDataArray != null && callerDataArray.length > 0) { StackTraceElement immediateCallerData = callerDataArray[0]; buf.append(" <log4j:locationInfo class=\""); buf.append(immediateCallerData.getClassName()); buf.append("\"\r\n"); buf.append(" method=\""); buf.append(Transform.escapeTags(immediateCallerData.getMethodName())); buf.append("\" file=\""); buf.append(Transform.escapeTags(immediateCallerData.getFileName())); buf.append("\" line=\""); buf.append(immediateCallerData.getLineNumber()); buf.append("\"/>\r\n"); } } if (this.getProperties()) { Map<String, String> propertyMap = event.getMDCPropertyMap(); if ((propertyMap != null) && (propertyMap.size() != 0)) { Set<Entry<String, String>> entrySet = propertyMap.entrySet(); buf.append(" <log4j:properties>"); for (Entry<String, String> entry : entrySet) { buf.append("\r\n <log4j:data"); buf.append(" name='" + Transform.escapeTags(entry.getKey()) + "'"); buf.append(" value='" + Transform.escapeTags(entry.getValue()) + "'"); buf.append(" />"); } buf.append("\r\n </log4j:properties>"); } } buf.append("\r\n</log4j:event>\r\n\r\n"); return buf.toString(); } }
XMLLayout extends LayoutBase<ILoggingEvent> { public String doLayout(ILoggingEvent event) { if (buf.capacity() > UPPER_LIMIT) { buf = new StringBuilder(DEFAULT_SIZE); } else { buf.setLength(0); } buf.append("<log4j:event logger=\""); buf.append(Transform.escapeTags(event.getLoggerName())); buf.append("\"\r\n"); buf.append(" timestamp=\""); buf.append(event.getTimeStamp()); buf.append("\" level=\""); buf.append(event.getLevel()); buf.append("\" thread=\""); buf.append(Transform.escapeTags(event.getThreadName())); buf.append("\">\r\n"); buf.append(" <log4j:message>"); buf.append(Transform.escapeTags(event.getFormattedMessage())); buf.append("</log4j:message>\r\n"); IThrowableProxy tp = event.getThrowableProxy(); if (tp != null) { StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray(); buf.append(" <log4j:throwable><![CDATA["); for (StackTraceElementProxy step : stepArray) { buf.append(CoreConstants.TAB); buf.append(step.toString()); buf.append("\r\n"); } buf.append("]]></log4j:throwable>\r\n"); } if (locationInfo) { StackTraceElement[] callerDataArray = event.getCallerData(); if (callerDataArray != null && callerDataArray.length > 0) { StackTraceElement immediateCallerData = callerDataArray[0]; buf.append(" <log4j:locationInfo class=\""); buf.append(immediateCallerData.getClassName()); buf.append("\"\r\n"); buf.append(" method=\""); buf.append(Transform.escapeTags(immediateCallerData.getMethodName())); buf.append("\" file=\""); buf.append(Transform.escapeTags(immediateCallerData.getFileName())); buf.append("\" line=\""); buf.append(immediateCallerData.getLineNumber()); buf.append("\"/>\r\n"); } } if (this.getProperties()) { Map<String, String> propertyMap = event.getMDCPropertyMap(); if ((propertyMap != null) && (propertyMap.size() != 0)) { Set<Entry<String, String>> entrySet = propertyMap.entrySet(); buf.append(" <log4j:properties>"); for (Entry<String, String> entry : entrySet) { buf.append("\r\n <log4j:data"); buf.append(" name='" + Transform.escapeTags(entry.getKey()) + "'"); buf.append(" value='" + Transform.escapeTags(entry.getValue()) + "'"); buf.append(" />"); } buf.append("\r\n </log4j:properties>"); } } buf.append("\r\n</log4j:event>\r\n\r\n"); return buf.toString(); }  }
XMLLayout extends LayoutBase<ILoggingEvent> { public String doLayout(ILoggingEvent event) { if (buf.capacity() > UPPER_LIMIT) { buf = new StringBuilder(DEFAULT_SIZE); } else { buf.setLength(0); } buf.append("<log4j:event logger=\""); buf.append(Transform.escapeTags(event.getLoggerName())); buf.append("\"\r\n"); buf.append(" timestamp=\""); buf.append(event.getTimeStamp()); buf.append("\" level=\""); buf.append(event.getLevel()); buf.append("\" thread=\""); buf.append(Transform.escapeTags(event.getThreadName())); buf.append("\">\r\n"); buf.append(" <log4j:message>"); buf.append(Transform.escapeTags(event.getFormattedMessage())); buf.append("</log4j:message>\r\n"); IThrowableProxy tp = event.getThrowableProxy(); if (tp != null) { StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray(); buf.append(" <log4j:throwable><![CDATA["); for (StackTraceElementProxy step : stepArray) { buf.append(CoreConstants.TAB); buf.append(step.toString()); buf.append("\r\n"); } buf.append("]]></log4j:throwable>\r\n"); } if (locationInfo) { StackTraceElement[] callerDataArray = event.getCallerData(); if (callerDataArray != null && callerDataArray.length > 0) { StackTraceElement immediateCallerData = callerDataArray[0]; buf.append(" <log4j:locationInfo class=\""); buf.append(immediateCallerData.getClassName()); buf.append("\"\r\n"); buf.append(" method=\""); buf.append(Transform.escapeTags(immediateCallerData.getMethodName())); buf.append("\" file=\""); buf.append(Transform.escapeTags(immediateCallerData.getFileName())); buf.append("\" line=\""); buf.append(immediateCallerData.getLineNumber()); buf.append("\"/>\r\n"); } } if (this.getProperties()) { Map<String, String> propertyMap = event.getMDCPropertyMap(); if ((propertyMap != null) && (propertyMap.size() != 0)) { Set<Entry<String, String>> entrySet = propertyMap.entrySet(); buf.append(" <log4j:properties>"); for (Entry<String, String> entry : entrySet) { buf.append("\r\n <log4j:data"); buf.append(" name='" + Transform.escapeTags(entry.getKey()) + "'"); buf.append(" value='" + Transform.escapeTags(entry.getValue()) + "'"); buf.append(" />"); } buf.append("\r\n </log4j:properties>"); } } buf.append("\r\n</log4j:event>\r\n\r\n"); return buf.toString(); }  @Override void start(); void setLocationInfo(boolean flag); boolean getLocationInfo(); void setProperties(final boolean flag); boolean getProperties(); String doLayout(ILoggingEvent event); @Override String getContentType(); }
XMLLayout extends LayoutBase<ILoggingEvent> { public String doLayout(ILoggingEvent event) { if (buf.capacity() > UPPER_LIMIT) { buf = new StringBuilder(DEFAULT_SIZE); } else { buf.setLength(0); } buf.append("<log4j:event logger=\""); buf.append(Transform.escapeTags(event.getLoggerName())); buf.append("\"\r\n"); buf.append(" timestamp=\""); buf.append(event.getTimeStamp()); buf.append("\" level=\""); buf.append(event.getLevel()); buf.append("\" thread=\""); buf.append(Transform.escapeTags(event.getThreadName())); buf.append("\">\r\n"); buf.append(" <log4j:message>"); buf.append(Transform.escapeTags(event.getFormattedMessage())); buf.append("</log4j:message>\r\n"); IThrowableProxy tp = event.getThrowableProxy(); if (tp != null) { StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray(); buf.append(" <log4j:throwable><![CDATA["); for (StackTraceElementProxy step : stepArray) { buf.append(CoreConstants.TAB); buf.append(step.toString()); buf.append("\r\n"); } buf.append("]]></log4j:throwable>\r\n"); } if (locationInfo) { StackTraceElement[] callerDataArray = event.getCallerData(); if (callerDataArray != null && callerDataArray.length > 0) { StackTraceElement immediateCallerData = callerDataArray[0]; buf.append(" <log4j:locationInfo class=\""); buf.append(immediateCallerData.getClassName()); buf.append("\"\r\n"); buf.append(" method=\""); buf.append(Transform.escapeTags(immediateCallerData.getMethodName())); buf.append("\" file=\""); buf.append(Transform.escapeTags(immediateCallerData.getFileName())); buf.append("\" line=\""); buf.append(immediateCallerData.getLineNumber()); buf.append("\"/>\r\n"); } } if (this.getProperties()) { Map<String, String> propertyMap = event.getMDCPropertyMap(); if ((propertyMap != null) && (propertyMap.size() != 0)) { Set<Entry<String, String>> entrySet = propertyMap.entrySet(); buf.append(" <log4j:properties>"); for (Entry<String, String> entry : entrySet) { buf.append("\r\n <log4j:data"); buf.append(" name='" + Transform.escapeTags(entry.getKey()) + "'"); buf.append(" value='" + Transform.escapeTags(entry.getValue()) + "'"); buf.append(" />"); } buf.append("\r\n </log4j:properties>"); } } buf.append("\r\n</log4j:event>\r\n\r\n"); return buf.toString(); }  @Override void start(); void setLocationInfo(boolean flag); boolean getLocationInfo(); void setProperties(final boolean flag); boolean getProperties(); String doLayout(ILoggingEvent event); @Override String getContentType();  }