Author: carnold Date: Thu Aug 9 15:02:51 2007 New Revision: 564399 URL: http://svn.apache.org/viewvc?view=rev&rev=564399 Log: Bug 43078: Optionally render MDC content in XMLLayout
Added: logging/log4j/branches/v1_2-branch/tests/witness/xmlLayout.mdc.1 - copied, changed from r563225, logging/log4j/trunk/tests/witness/xml/xmlLayout.mdc.1 logging/log4j/branches/v1_2-branch/tests/witness/xmlLayout.mdc.2 - copied, changed from r563225, logging/log4j/trunk/tests/witness/xml/xmlLayout.mdc.2 Modified: logging/log4j/branches/v1_2-branch/src/changes/changes.xml logging/log4j/branches/v1_2-branch/src/main/java/org/apache/log4j/xml/XMLLayout.java logging/log4j/branches/v1_2-branch/src/main/resources/org/apache/log4j/xml/log4j.dtd logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/util/JunitTestRunnerFilter.java logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/xml/XMLLayoutTestCase.java Modified: logging/log4j/branches/v1_2-branch/src/changes/changes.xml URL: http://svn.apache.org/viewvc/logging/log4j/branches/v1_2-branch/src/changes/changes.xml?view=diff&rev=564399&r1=564398&r2=564399 ============================================================================== --- logging/log4j/branches/v1_2-branch/src/changes/changes.xml (original) +++ logging/log4j/branches/v1_2-branch/src/changes/changes.xml Thu Aug 9 15:02:51 2007 @@ -22,6 +22,7 @@ <body> <release version="1.2.15" date="2007-06-27" description="SyslogAppender enhancements, NTEventLogAppender and Maven build."> + <action action="add" issue="43078">Optionally render MDC content in XMLLayout</action> <action action="fix" issue="42694">Typo in log4j.dtd concerning threshold.</action> <action action="fix" issue="42611">ERFATestCase fails on some JDK's.</action> <action action="fix" issue="42585">SocketNode can leak Sockets.</action> Modified: logging/log4j/branches/v1_2-branch/src/main/java/org/apache/log4j/xml/XMLLayout.java URL: http://svn.apache.org/viewvc/logging/log4j/branches/v1_2-branch/src/main/java/org/apache/log4j/xml/XMLLayout.java?view=diff&rev=564399&r1=564398&r2=564399 ============================================================================== --- logging/log4j/branches/v1_2-branch/src/main/java/org/apache/log4j/xml/XMLLayout.java (original) +++ logging/log4j/branches/v1_2-branch/src/main/java/org/apache/log4j/xml/XMLLayout.java Thu Aug 9 15:02:51 2007 @@ -24,6 +24,9 @@ import org.apache.log4j.spi.LocationInfo; import org.apache.log4j.spi.LoggingEvent; +import java.util.Set; +import java.util.Arrays; + /** * The output of the XMLLayout consists of a series of log4j:event * elements as defined in the <a @@ -64,6 +67,7 @@ private StringBuffer buf = new StringBuffer(DEFAULT_SIZE); private boolean locationInfo = false; + private boolean properties = false; /** * The <b>LocationInfo</b> option takes a boolean value. By default, @@ -86,7 +90,23 @@ public boolean getLocationInfo() { return locationInfo; } - + + /** + * Sets whether MDC key-value pairs should be output, default false. + * @param flag new value. + */ + public void setProperties(final boolean flag) { + properties = flag; + } + + /** + * Gets whether MDC key-value pairs should be output. + * @return true if MDC key-value pairs are output. + */ + public boolean getProperties() { + return properties; + } + /** No options to activate. */ public void activateOptions() { } @@ -151,6 +171,27 @@ buf.append("\" line=\""); buf.append(locationInfo.getLineNumber()); buf.append("\"/>\r\n"); + } + + if (properties) { + Set keySet = event.getPropertyKeySet(); + if (keySet.size() > 0) { + buf.append("<log4j:properties>\r\n"); + Object[] keys = keySet.toArray(); + Arrays.sort(keys); + for (int i = 0; i < keys.length; i++) { + String key = keys[i].toString(); + Object val = event.getMDC(key); + if (val != null) { + buf.append("<log4j:data name=\""); + buf.append(Transform.escapeTags(key)); + buf.append("\" value=\""); + buf.append(Transform.escapeTags(val.toString())); + buf.append("\"/>\r\n"); + } + } + buf.append("</log4j:properties>\r\n"); + } } buf.append("</log4j:event>\r\n\r\n"); Modified: logging/log4j/branches/v1_2-branch/src/main/resources/org/apache/log4j/xml/log4j.dtd URL: http://svn.apache.org/viewvc/logging/log4j/branches/v1_2-branch/src/main/resources/org/apache/log4j/xml/log4j.dtd?view=diff&rev=564399&r1=564398&r2=564399 ============================================================================== --- logging/log4j/branches/v1_2-branch/src/main/resources/org/apache/log4j/xml/log4j.dtd (original) +++ logging/log4j/branches/v1_2-branch/src/main/resources/org/apache/log4j/xml/log4j.dtd Thu Aug 9 15:02:51 2007 @@ -1,4 +1,20 @@ <?xml version="1.0" encoding="UTF-8" ?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> <!-- Authors: Chris Taylor, Ceki Gulcu. --> @@ -137,12 +153,12 @@ <!ELEMENT connectionSource (dataSource?, param*)> <!ATTLIST connectionSource - class CDATA #REQUIRED + class CDATA #REQUIRED > <!ELEMENT dataSource (param*)> <!ATTLIST dataSource - class CDATA #REQUIRED + class CDATA #REQUIRED > <!ELEMENT triggeringPolicy ((param|filter)*)> @@ -177,7 +193,7 @@ <!ELEMENT log4j:event (log4j:message, log4j:NDC?, log4j:throwable?, - log4j:locationInfo?) > + log4j:locationInfo?, log4j:properties?) > <!-- The timestamp format is application dependent. --> <!ATTLIST log4j:event @@ -185,6 +201,7 @@ level CDATA #REQUIRED thread CDATA #REQUIRED timestamp CDATA #REQUIRED + time CDATA #IMPLIED > <!ELEMENT log4j:message (#PCDATA)> @@ -198,4 +215,12 @@ method CDATA #REQUIRED file CDATA #REQUIRED line CDATA #REQUIRED +> + +<!ELEMENT log4j:properties (log4j:data*)> + +<!ELEMENT log4j:data EMPTY> +<!ATTLIST log4j:data + name CDATA #REQUIRED + value CDATA #REQUIRED > Modified: logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/util/JunitTestRunnerFilter.java URL: http://svn.apache.org/viewvc/logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/util/JunitTestRunnerFilter.java?view=diff&rev=564399&r1=564398&r2=564399 ============================================================================== --- logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/util/JunitTestRunnerFilter.java (original) +++ logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/util/JunitTestRunnerFilter.java Thu Aug 9 15:02:51 2007 @@ -40,6 +40,11 @@ "/at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner/", in)) { return null; + } else if ( + util.match( + "/at com.intellij/", + in)) { + return null; } else if (in.indexOf("at junit.") >= 0 && in.indexOf("ui.TestRunner") >= 0) { return null; } else if (in.indexOf("org.apache.maven") >= 0) { Modified: logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/xml/XMLLayoutTestCase.java URL: http://svn.apache.org/viewvc/logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/xml/XMLLayoutTestCase.java?view=diff&rev=564399&r1=564398&r2=564399 ============================================================================== --- logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/xml/XMLLayoutTestCase.java (original) +++ logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/xml/XMLLayoutTestCase.java Thu Aug 9 15:02:51 2007 @@ -23,6 +23,7 @@ import org.apache.log4j.FileAppender; import org.apache.log4j.Level; import org.apache.log4j.Logger; +import org.apache.log4j.MDC; import org.apache.log4j.util.Compare; import org.apache.log4j.util.Filter; import org.apache.log4j.util.JunitTestRunnerFilter; @@ -32,6 +33,8 @@ import org.apache.log4j.util.XMLLineAttributeFilter; import org.apache.log4j.util.XMLTimestampFilter; +import java.util.Hashtable; + public class XMLLayoutTestCase extends TestCase { static String TEMP = "output/temp"; @@ -138,6 +141,54 @@ new SunReflectFilter()}); assertTrue(Compare.compare(FILTERED, "witness/xmlLayout.null")); } + + /** + * Tests the format of the MDC portion of the layout to ensure + * the key-value pairs we put in turn up in the output file. + * @throws Exception + */ + public void testMDC() throws Exception { + XMLLayout xmlLayout = new XMLLayout(); + xmlLayout.setProperties(true); + root.addAppender(new FileAppender(xmlLayout, TEMP, false)); + + Hashtable context = MDC.getContext(); + if (context != null) { + context.clear(); + } + MDC.put("key1", "val1"); + MDC.put("key2", "val2"); + + logger.debug("Hello"); + Transformer.transform( + TEMP, FILTERED, + new Filter[] { new LineNumberFilter(), + new JunitTestRunnerFilter(), + new XMLTimestampFilter()}); + assertTrue(Compare.compare(FILTERED, "witness/xmlLayout.mdc.1")); + } + + public void testMDCEscaped() throws Exception { + XMLLayout xmlLayout = new XMLLayout(); + xmlLayout.setProperties(true); + root.addAppender(new FileAppender(xmlLayout, TEMP, false)); + + Hashtable context = MDC.getContext(); + if (context != null) { + context.clear(); + } + MDC.put("blahAttribute", "<blah value='blah'>"); + MDC.put("<blahKey value='blah'/>", "blahValue"); + + logger.debug("Hello"); + Transformer.transform( + TEMP, FILTERED, + new Filter[] { new LineNumberFilter(), + new JunitTestRunnerFilter(), + new XMLTimestampFilter() }); + assertTrue(Compare.compare(FILTERED, "witness/xmlLayout.mdc.2")); + } + void common() { String oldThreadName = Thread.currentThread().getName(); @@ -182,6 +233,8 @@ suite.addTest(new XMLLayoutTestCase("locationInfo")); suite.addTest(new XMLLayoutTestCase("testCDATA")); suite.addTest(new XMLLayoutTestCase("testNull")); + suite.addTest(new XMLLayoutTestCase("testMDC")); + suite.addTest(new XMLLayoutTestCase("testMDCEscaped")); return suite; } Copied: logging/log4j/branches/v1_2-branch/tests/witness/xmlLayout.mdc.1 (from r563225, logging/log4j/trunk/tests/witness/xml/xmlLayout.mdc.1) URL: http://svn.apache.org/viewvc/logging/log4j/branches/v1_2-branch/tests/witness/xmlLayout.mdc.1?view=diff&rev=564399&p1=logging/log4j/trunk/tests/witness/xml/xmlLayout.mdc.1&r1=563225&p2=logging/log4j/branches/v1_2-branch/tests/witness/xmlLayout.mdc.1&r2=564399 ============================================================================== Binary files - no diff available. Copied: logging/log4j/branches/v1_2-branch/tests/witness/xmlLayout.mdc.2 (from r563225, logging/log4j/trunk/tests/witness/xml/xmlLayout.mdc.2) URL: http://svn.apache.org/viewvc/logging/log4j/branches/v1_2-branch/tests/witness/xmlLayout.mdc.2?view=diff&rev=564399&p1=logging/log4j/trunk/tests/witness/xml/xmlLayout.mdc.2&r1=563225&p2=logging/log4j/branches/v1_2-branch/tests/witness/xmlLayout.mdc.2&r2=564399 ============================================================================== Binary files - no diff available. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]