You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2013/08/16 22:54:00 UTC

svn commit: r1514893 - in /logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core: helpers/Throwables.java layout/JSONLayout.java layout/XMLLayout.java

Author: ggregory
Date: Fri Aug 16 20:53:59 2013
New Revision: 1514893

URL: http://svn.apache.org/r1514893
Log:
[LOG4J2-356] Create a JSON Layout. Refactor a duplicate method from the JSON and XML layouts into Throwables.getThrowableStringList(throwable);

Added:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Throwables.java   (with props)
Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/JSONLayout.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java

Added: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Throwables.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Throwables.java?rev=1514893&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Throwables.java (added)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Throwables.java Fri Aug 16 20:53:59 2013
@@ -0,0 +1,66 @@
+/*
+ * 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.helpers;
+
+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.util.ArrayList;
+import java.util.List;
+
+/**
+ * Helps with Throwable objects.
+ */
+public class Throwables {
+
+    /**
+     * Converts a Throwable into a List of Strings
+     * 
+     * @param throwable
+     *            the Throwable
+     * @return a List of Strings
+     */
+    public static List<String> toStringList(final Throwable throwable) {
+        final StringWriter sw = new StringWriter();
+        final PrintWriter pw = new PrintWriter(sw);
+        try {
+            throwable.printStackTrace(pw);
+        } catch (final RuntimeException ex) {
+            // Ignore any exceptions.
+        }
+        pw.flush();
+        final LineNumberReader reader = new LineNumberReader(new StringReader(sw.toString()));
+        final ArrayList<String> lines = new ArrayList<String>();
+        try {
+            String line = reader.readLine();
+            while (line != null) {
+                lines.add(line);
+                line = reader.readLine();
+            }
+        } catch (final IOException ex) {
+            if (ex instanceof InterruptedIOException) {
+                Thread.currentThread().interrupt();
+            }
+            lines.add(ex.toString());
+        }
+        return lines;
+    }
+
+}

Propchange: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Throwables.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Throwables.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/JSONLayout.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/JSONLayout.java?rev=1514893&r1=1514892&r2=1514893&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/JSONLayout.java (original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/JSONLayout.java Fri Aug 16 20:53:59 2013
@@ -16,14 +16,7 @@
  */
 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.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -35,6 +28,7 @@ import org.apache.logging.log4j.core.con
 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.Charsets;
+import org.apache.logging.log4j.core.helpers.Throwables;
 import org.apache.logging.log4j.core.helpers.Transform;
 import org.apache.logging.log4j.message.Message;
 import org.apache.logging.log4j.message.MultiformatMessage;
@@ -214,7 +208,7 @@ public class JSONLayout extends Abstract
             buf.append(this.eol);
             buf.append(this.indent2);
             buf.append("\"throwable\":\"");
-            final List<String> list = this.getThrowableStringList(throwable);
+            final List<String> list = Throwables.toStringList(throwable);
             for (final String str : list) {
                 buf.append(Transform.escapeJsonControlCharacters(str));
                 buf.append("\\\\n");
@@ -347,32 +341,6 @@ public class JSONLayout extends Abstract
         return "application/json; charset=" + this.getCharset();
     }
 
-    private List<String> getThrowableStringList(final Throwable throwable) {
-        final StringWriter sw = new StringWriter();
-        final PrintWriter pw = new PrintWriter(sw);
-        try {
-            throwable.printStackTrace(pw);
-        } catch (final RuntimeException ex) {
-            // Ignore any exceptions.
-        }
-        pw.flush();
-        final LineNumberReader reader = new LineNumberReader(new StringReader(sw.toString()));
-        final ArrayList<String> lines = new ArrayList<String>();
-        try {
-            String line = reader.readLine();
-            while (line != null) {
-                lines.add(line);
-                line = reader.readLine();
-            }
-        } catch (final IOException ex) {
-            if (ex instanceof InterruptedIOException) {
-                Thread.currentThread().interrupt();
-            }
-            lines.add(ex.toString());
-        }
-        return lines;
-    }
-
     /**
      * Creates an XML Layout.
      * 

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java?rev=1514893&r1=1514892&r2=1514893&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java (original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java Fri Aug 16 20:53:59 2013
@@ -16,14 +16,7 @@
  */
 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.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -34,6 +27,7 @@ import org.apache.logging.log4j.core.con
 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
 import org.apache.logging.log4j.core.helpers.Charsets;
 import org.apache.logging.log4j.core.helpers.Strings;
+import org.apache.logging.log4j.core.helpers.Throwables;
 import org.apache.logging.log4j.core.helpers.Transform;
 import org.apache.logging.log4j.message.Message;
 import org.apache.logging.log4j.message.MultiformatMessage;
@@ -203,7 +197,7 @@ public class XMLLayout extends AbstractS
 
         final Throwable throwable = event.getThrown();
         if (throwable != null) {
-            final List<String> s = getThrowableStringList(throwable);
+            final List<String> s = Throwables.toStringList(throwable);
             buf.append(this.indent2);
             buf.append('<');
             if (!complete) {
@@ -346,32 +340,6 @@ public class XMLLayout extends AbstractS
         return "text/xml; charset=" + this.getCharset();
     }
 
-    private List<String> getThrowableStringList(final Throwable throwable) {
-        final StringWriter sw = new StringWriter();
-        final PrintWriter pw = new PrintWriter(sw);
-        try {
-            throwable.printStackTrace(pw);
-        } catch (final RuntimeException ex) {
-            // Ignore any exceptions.
-        }
-        pw.flush();
-        final LineNumberReader reader = new LineNumberReader(new StringReader(sw.toString()));
-        final ArrayList<String> lines = new ArrayList<String>();
-        try {
-          String line = reader.readLine();
-          while (line != null) {
-            lines.add(line);
-            line = reader.readLine();
-          }
-        } catch (final IOException ex) {
-            if (ex instanceof InterruptedIOException) {
-                Thread.currentThread().interrupt();
-            }
-            lines.add(ex.toString());
-        }
-        return lines;
-    }
-
     /**
      * Creates an XML Layout.
      *