logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1127788 - in /logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src: main/java/org/apache/logging/log4j/core/layout/ test/java/org/apache/logging/log4j/core/layout/
Date Thu, 26 May 2011 05:49:14 GMT
Author: rgoers
Date: Thu May 26 05:49:13 2011
New Revision: 1127788

URL: http://svn.apache.org/viewvc?rev=1127788&view=rev
Log:
Add XML Layout. Change HTMLLayoutTest to try to get it to pass

Added:
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/layout/XMLLayoutTest.java
      - copied, changed from r1127414, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/layout/HTMLLayoutTest.java
Modified:
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/layout/HTMLLayoutTest.java

Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java?rev=1127788&view=auto
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java
(added)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java
Thu May 26 05:49:13 2011
@@ -0,0 +1,244 @@
+/*
+ * 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.
+ */
+package org.apache.logging.log4j.core.layout;
+
+import java.io.IOException;
+import java.io.InterruptedIOException;
+import java.io.LineNumberReader;
+import java.io.PrintWriter;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginAttr;
+import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+import org.apache.logging.log4j.core.helpers.Transform;
+import org.apache.logging.log4j.core.LogEvent;
+
+
+/**
+ * The output of the XMLLayout consists of a series of log4j:event
+ * elements as defined in the <a href="log4j.dtd">log4j.dtd</a>. If configured
to do so it will
+ * output a complete well-formed XML file. The output is designed to be
+ * included as an <em>external entity</em> in a separate file to form
+ * a correct XML file.
+ * <p/>
+ * <p>For example, if <code>abc</code> is the name of the file where
+ * the XMLLayout ouput goes, then a well-formed XML file would be:
+ * <p/>
+ * <pre>
+ * &lt;?xml version="1.0" ?&gt;
+ *
+ * &lt;!DOCTYPE log4j:eventSet SYSTEM "log4j.dtd" [&lt;!ENTITY data SYSTEM "abc"&gt;]&gt;
+ *
+ * &lt;log4j:eventSet version="1.2" xmlns:log4j="http://jakarta.apache.org/log4j/"&gt;
+ * &nbsp;&nbsp;&data;
+ * &lt;/log4j:eventSet&gt;
+ * </pre>
+ * <p/>
+ * <p>This approach enforces the independence of the XMLLayout and the
+ * appender where it is embedded.
+ * <p/>
+ * <p>The <code>version</code> attribute helps components to correctly
+ * intrepret output generated by XMLLayout. The value of this
+ * attribute should be "1.1" for output generated by log4j versions
+ * prior to log4j 1.2 (final release) and "1.2" for relase 1.2 and
+ * later.
+ * <p/>
+ * Appenders using this layout should have their encoding
+ * set to UTF-8 or UTF-16, otherwise events containing
+ * non ASCII characters could result in corrupted
+ * log files.
+ */
+@Plugin(name="XMLLayout",type="Core",elementType="layout",printObject=true)
+public class XMLLayout extends LayoutBase {
+
+    private static final int DEFAULT_SIZE = 256;
+    private static final int UPPER_LIMIT = 2048;
+
+    private StringBuilder buf = new StringBuilder(DEFAULT_SIZE);
+    private final boolean locationInfo;
+    private final boolean properties;
+    private final boolean complete;
+
+    protected final Charset charset;
+
+    public XMLLayout(boolean locationInfo, boolean properties, boolean complete, Charset
charset) {
+        this.locationInfo = locationInfo;
+        this.charset = charset;
+        this.properties = properties;
+        this.complete = complete;
+    }
+
+    /**
+     * Formats a {@link org.apache.logging.log4j.core.LogEvent} in conformance with the log4j.dtd.
+     */
+    public byte[] format(final LogEvent event) {
+
+        // Reset working buffer. If the buffer is too large, then we need a new
+        // one in order to avoid the penalty of creating a large array.
+        if (buf.capacity() > UPPER_LIMIT) {
+            buf = new StringBuilder(DEFAULT_SIZE);
+        } else {
+            buf.setLength(0);
+        }
+
+        // We yield to the \r\n heresy.
+
+        buf.append("<log4j:event logger=\"");
+        String name = event.getLoggerName();
+        if (name.length() == 0) {
+            name = "root";
+        }
+        buf.append(Transform.escapeTags(name));
+        buf.append("\" timestamp=\"");
+        buf.append(event.getMillis());
+        buf.append("\" level=\"");
+        buf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
+        buf.append("\" thread=\"");
+        buf.append(Transform.escapeTags(event.getThreadName()));
+        buf.append("\">\r\n");
+
+        buf.append("<log4j:message><![CDATA[");
+        // Append the rendered message. Also make sure to escape any
+        // existing CDATA sections.
+        Transform.appendEscapingCDATA(buf, event.getMessage().getFormattedMessage());
+        buf.append("]]></log4j:message>\r\n");
+
+        if (event.getContextStack().size() > 0) {
+            buf.append("<log4j:NDC><![CDATA[");
+            Transform.appendEscapingCDATA(buf, event.getContextStack().toString());
+            buf.append("]]></log4j:NDC>\r\n");
+        }
+
+        Throwable throwable = event.getThrown();
+        if (throwable != null) {
+            List<String> s = getThrowableString(throwable);
+            buf.append("<log4j:throwable><![CDATA[");
+            for (String str : s) {
+                Transform.appendEscapingCDATA(buf, str);
+                buf.append("\r\n");
+            }
+            buf.append("]]></log4j:throwable>\r\n");
+        }
+
+        if (locationInfo) {
+            StackTraceElement element = event.getSource();
+            buf.append("<log4j:locationInfo class=\"");
+            buf.append(Transform.escapeTags(element.getClassName()));
+            buf.append("\" method=\"");
+            buf.append(Transform.escapeTags(element.getMethodName()));
+            buf.append("\" file=\"");
+            buf.append(Transform.escapeTags(element.getFileName()));
+            buf.append("\" line=\"");
+            buf.append(element.getLineNumber());
+            buf.append("\"/>\r\n");
+        }
+
+        if (properties && event.getContextMap().size() > 0) {
+            buf.append("<log4j:properties>\r\n");
+            for (Map.Entry<String, Object> entry : event.getContextMap().entrySet())
{
+                buf.append("<log4j:data name=\"");
+                buf.append(Transform.escapeTags(entry.getKey()));
+                buf.append("\" value=\"");
+                buf.append(Transform.escapeTags(String.valueOf(entry.getValue())));
+                buf.append("\"/>\r\n");
+            }
+            buf.append("</log4j:properties>\r\n");
+        }
+
+        buf.append("</log4j:event>\r\n\r\n");
+
+        return buf.toString().getBytes(charset);
+    }
+
+    /**
+     * Returns appropriate XML headers.
+     */
+    @Override
+    public byte[] getHeader() {
+        if (!complete) {
+            return null;
+        }
+        StringBuilder sbuf = new StringBuilder();
+        sbuf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
+        sbuf.append("<log4j:eventSet xmlns:log4j=\"http://jakarta.apache.org/log4j/\">\r\n");
+        return sbuf.toString().getBytes(charset);
+    }
+
+
+    /**
+     * Returns appropriate XML headers.
+     */
+    @Override
+    public byte[] getFooter() {
+        if (!complete) {
+            return null;
+        }
+        StringBuilder sbuf = new StringBuilder();
+        sbuf.append("</log4j:eventSet>\r\n");
+        return sbuf.toString().getBytes(charset);
+    }
+
+    List<String> getThrowableString(Throwable throwable) {
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        try {
+            throwable.printStackTrace(pw);
+        } catch(RuntimeException ex) {
+        }
+        pw.flush();
+        LineNumberReader reader = new LineNumberReader(new StringReader(sw.toString()));
+        ArrayList<String> lines = new ArrayList<String>();
+        try {
+          String line = reader.readLine();
+          while(line != null) {
+            lines.add(line);
+            line = reader.readLine();
+          }
+        } catch(IOException ex) {
+            if (ex instanceof InterruptedIOException) {
+                Thread.currentThread().interrupt();
+            }
+            lines.add(ex.toString());
+        }
+        return lines;
+    }
+
+    @PluginFactory
+    public static XMLLayout createLayout(@PluginAttr("locationInfo") String locationInfo,
+                                         @PluginAttr("properties") String properties,
+                                         @PluginAttr("complete") String complete,
+                                         @PluginAttr("charset") String charset) {
+        Charset c = Charset.isSupported("UTF-8") ? Charset.forName("UTF-8") : Charset.defaultCharset();
+        if (charset != null) {
+            if (Charset.isSupported(charset)) {
+                c = Charset.forName(charset);
+            } else {
+                logger.error("Charset " + charset + " is not supported for layout, using
" + c.displayName());
+            }
+        }
+        boolean info = locationInfo == null ? false : Boolean.valueOf(locationInfo);
+        boolean props = properties == null ? false : Boolean.valueOf(properties);
+        boolean comp = complete == null ? false : Boolean.valueOf(complete);
+        return new XMLLayout(info, props, comp, c);
+    }
+}

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/layout/HTMLLayoutTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/layout/HTMLLayoutTest.java?rev=1127788&r1=1127787&r2=1127788&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/layout/HTMLLayoutTest.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/layout/HTMLLayoutTest.java
Thu May 26 05:49:13 2011
@@ -96,9 +96,9 @@ public class HTMLLayoutTest {
 
         List<String> list = appender.getMessages();
 
-        assertTrue("Incorrect number of lines. Expected 93, actual " + list.size(), list.size()
== 93);
+        assertTrue("Incorrect number of lines. Require at least 85 " + list.size(), list.size()
> 85);
         assertTrue("Incorrect header", list.get(3).equals("<title>Log4J Log Messages</title>"));
-        assertTrue("Incorrect footer", list.get(92).equals("</body></html>"));
+        assertTrue("Incorrect footer", list.get(list.size() - 1).equals("</body></html>"));
         assertTrue("Incorrect body", list.get(61).equals(body));
     }
 }

Copied: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/layout/XMLLayoutTest.java
(from r1127414, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/layout/HTMLLayoutTest.java)
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/layout/XMLLayoutTest.java?p2=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/layout/XMLLayoutTest.java&p1=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/layout/HTMLLayoutTest.java&r1=1127414&r2=1127788&rev=1127788&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/layout/HTMLLayoutTest.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/layout/XMLLayoutTest.java
Thu May 26 05:49:13 2011
@@ -23,16 +23,12 @@ import org.apache.logging.log4j.core.Bas
 import org.apache.logging.log4j.core.Logger;
 import org.apache.logging.log4j.core.LoggerContext;
 import org.apache.logging.log4j.core.appender.ConsoleAppender;
-import org.apache.logging.log4j.core.appender.FileAppender;
-import org.apache.logging.log4j.core.appender.FileManager;
 import org.apache.logging.log4j.core.appender.ListAppender;
 import org.apache.logging.log4j.core.config.ConfigurationFactory;
-import org.apache.logging.log4j.core.util.Compare;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
-import java.io.FileOutputStream;
 import java.util.List;
 
 import static org.junit.Assert.assertTrue;
@@ -40,7 +36,7 @@ import static org.junit.Assert.assertTru
 /**
  *
  */
-public class HTMLLayoutTest {
+public class XMLLayoutTest {
     LoggerContext ctx = (LoggerContext) LogManager.getContext();
     Logger root = ctx.getLogger("");
 
@@ -59,7 +55,7 @@ public class HTMLLayoutTest {
     }
 
     private static final String body =
-        "<tr><td bgcolor=\"#993300\" style=\"color:White; font-size : xx-small;\"
colspan=\"6\">java.lang.NullPointerException: test";
+        "<log4j:message><![CDATA[empty mdc]]></log4j:message>\r";
 
 
     /**
@@ -69,7 +65,7 @@ public class HTMLLayoutTest {
     public void testLayout() throws Exception {
 
         // set up appender
-        HTMLLayout layout = HTMLLayout.createLayout("true", null, null, null);
+        XMLLayout layout = XMLLayout.createLayout("true", "true", "true", null);
         ListAppender appender = new ListAppender("List", null, layout, true);
         appender.start();
 
@@ -96,9 +92,9 @@ public class HTMLLayoutTest {
 
         List<String> list = appender.getMessages();
 
-        assertTrue("Incorrect number of lines. Expected 93, actual " + list.size(), list.size()
== 93);
-        assertTrue("Incorrect header", list.get(3).equals("<title>Log4J Log Messages</title>"));
-        assertTrue("Incorrect footer", list.get(92).equals("</body></html>"));
-        assertTrue("Incorrect body", list.get(61).equals(body));
+        assertTrue("Incorrect number of lines. Require at least 50 " + list.size(), list.size()
> 50);
+        assertTrue("Incorrect header", list.get(0).equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r"));
+        assertTrue("Incorrect footer", list.get(list.size() - 1).equals("</log4j:eventSet>\r"));
+        assertTrue("Incorrect body. Expected " + body + " Actual: " + list.get(8), list.get(8).equals(body));
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Mime
View raw message