You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by mi...@apache.org on 2016/04/26 14:40:50 UTC

logging-log4j2 git commit: Option to not log stacktraces for logged Throwables in GelfLayout

Repository: logging-log4j2
Updated Branches:
  refs/heads/master 988ed5093 -> d1a737532


Option to not log stacktraces for logged Throwables in GelfLayout


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/d1a73753
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/d1a73753
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/d1a73753

Branch: refs/heads/master
Commit: d1a737532ed6bc70cce1ed43fb88a916574823c2
Parents: 988ed50
Author: Mikael Ståldal <mi...@magine.com>
Authored: Tue Apr 26 14:29:28 2016 +0200
Committer: Mikael Ståldal <mi...@magine.com>
Committed: Tue Apr 26 14:40:23 2016 +0200

----------------------------------------------------------------------
 .../logging/log4j/core/layout/GelfLayout.java   | 23 +++++++++-----
 .../log4j/core/layout/GelfLayoutTest.java       | 17 ++++++----
 .../log4j/perf/jmh/GelfLayoutBenchmark.java     |  3 +-
 src/changes/changes.xml                         |  3 ++
 src/site/xdoc/manual/layouts.xml.vm             | 33 ++++++++++++++++++--
 5 files changed, 61 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d1a73753/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/GelfLayout.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/GelfLayout.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/GelfLayout.java
index cb733c9..7db739e 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/GelfLayout.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/GelfLayout.java
@@ -65,8 +65,7 @@ import java.util.zip.GZIPOutputStream;
  * &lt;/Appenders&gt;
  * </pre>
  *
- * @see <a href="http://graylog2.org/resources/gelf/specification">GELF
- *      specification</a>
+ * @see <a href="http://docs.graylog.org/en/latest/pages/gelf.html#gelf">GELF specification</a>
  */
 @Plugin(name = "GelfLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true)
 public final class GelfLayout extends AbstractStringLayout {
@@ -105,14 +104,16 @@ public final class GelfLayout extends AbstractStringLayout {
     private final int compressionThreshold;
     private final CompressionType compressionType;
     private final String host;
+    private final boolean includeStacktrace;
 
     public GelfLayout(final String host, final KeyValuePair[] additionalFields, final CompressionType compressionType,
-            final int compressionThreshold) {
+                      final int compressionThreshold, boolean includeStacktrace) {
         super(StandardCharsets.UTF_8);
         this.host = host;
         this.additionalFields = additionalFields;
         this.compressionType = compressionType;
         this.compressionThreshold = compressionThreshold;
+        this.includeStacktrace = includeStacktrace;
     }
     
     @PluginFactory
@@ -123,9 +124,11 @@ public final class GelfLayout extends AbstractStringLayout {
             @PluginAttribute(value = "compressionType",
                 defaultString = "GZIP") final CompressionType compressionType,
             @PluginAttribute(value = "compressionThreshold",
-                defaultInt= COMPRESSION_THRESHOLD) final int compressionThreshold) {
+                defaultInt = COMPRESSION_THRESHOLD) final int compressionThreshold,
+            @PluginAttribute(value = "includeStacktrace",
+                defaultBoolean = true) final boolean includeStacktrace) {
             // @formatter:on
-        return new GelfLayout(host, additionalFields, compressionType, compressionThreshold);
+        return new GelfLayout(host, additionalFields, compressionType, compressionThreshold, includeStacktrace);
     }
 
     @Override
@@ -214,7 +217,11 @@ public final class GelfLayout extends AbstractStringLayout {
         }
         if (event.getThrown() != null) {
             builder.append("\"full_message\":\"");
-            JsonUtils.quoteAsString(formatThrowable(event.getThrown()), builder);
+            if (includeStacktrace) {
+                JsonUtils.quoteAsString(formatThrowable(event.getThrown()), builder);
+            } else {
+                JsonUtils.quoteAsString(event.getThrown().toString(), builder);
+            }
             builder.append(QC);
         }
 
@@ -285,12 +292,12 @@ public final class GelfLayout extends AbstractStringLayout {
     /**
      * Non-private to make it accessible from unit test.
      */
-    static String formatThrowable(final Throwable throwable) {
+    static CharSequence formatThrowable(final Throwable throwable) {
         // stack traces are big enough to provide a reasonably large initial capacity here
         final StringWriter sw = new StringWriter(2048);
         final PrintWriter pw = new PrintWriter(sw);
         throwable.printStackTrace(pw);
         pw.flush();
-        return sw.toString();
+        return sw.getBuffer();
     }
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d1a73753/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/GelfLayoutTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/GelfLayoutTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/GelfLayoutTest.java
index d3e66d6..d12aa57 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/GelfLayoutTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/GelfLayoutTest.java
@@ -74,14 +74,14 @@ public class GelfLayoutTest {
 
     Logger root = ctx.getLogger("");
 
-    private void testCompressedLayout(final CompressionType compressionType) throws IOException {
+    private void testCompressedLayout(final CompressionType compressionType, boolean includeStacktrace) throws IOException {
         for (final Appender appender : root.getAppenders().values()) {
             root.removeAppender(appender);
         }
         // set up appenders
         final GelfLayout layout = GelfLayout.createLayout(HOSTNAME, new KeyValuePair[] {
                 new KeyValuePair(KEY1, VALUE1),
-                new KeyValuePair(KEY2, VALUE2), }, compressionType, 1024);
+                new KeyValuePair(KEY2, VALUE2), }, compressionType, 1024, includeStacktrace);
         final ListAppender eventAppender = new ListAppender("Events", null, null, true, false);
         final ListAppender rawAppender = new ListAppender("Raw", null, layout, true, true);
         final ListAppender formattedAppender = new ListAppender("Formatted", null, layout, true, false);
@@ -185,7 +185,7 @@ public class GelfLayoutTest {
                 "\"_logger\": \"\"," +
                 "\"short_message\": \"" + LINE3 + "\"," +
                 "\"full_message\": \"" + String.valueOf(JsonStringEncoder.getInstance().quoteAsString(
-                GelfLayout.formatThrowable(exception))) + "\"," +
+                includeStacktrace ? GelfLayout.formatThrowable(exception).toString() : exception.toString())) + "\"," +
                 "\"_" + KEY1 + "\": \"" + VALUE1 + "\"," +
                 "\"_" + KEY2 + "\": \"" + VALUE2 + "\"," +
                 "\"_" + MDCKEY1 + "\": \"" + MDCVALUE1 + "\"," +
@@ -198,17 +198,22 @@ public class GelfLayoutTest {
 
     @Test
     public void testLayoutGzipCompression() throws Exception {
-        testCompressedLayout(CompressionType.GZIP);
+        testCompressedLayout(CompressionType.GZIP, true);
     }
 
     @Test
     public void testLayoutNoCompression() throws Exception {
-        testCompressedLayout(CompressionType.OFF);
+        testCompressedLayout(CompressionType.OFF, true);
     }
 
     @Test
     public void testLayoutZlibCompression() throws Exception {
-        testCompressedLayout(CompressionType.ZLIB);
+        testCompressedLayout(CompressionType.ZLIB, true);
+    }
+
+    @Test
+    public void testLayoutNoStacktrace() throws Exception {
+        testCompressedLayout(CompressionType.OFF, false);
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d1a73753/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/GelfLayoutBenchmark.java
----------------------------------------------------------------------
diff --git a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/GelfLayoutBenchmark.java b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/GelfLayoutBenchmark.java
index ee2f143..041be85 100644
--- a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/GelfLayoutBenchmark.java
+++ b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/GelfLayoutBenchmark.java
@@ -82,7 +82,8 @@ public class GelfLayoutBenchmark {
                 "host",
                 ADDITIONAL_FIELDS,
                 GelfLayout.CompressionType.OFF,
-                0));
+                0,
+                true));
 
         j = 0;
     }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d1a73753/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c5831c0..bf193a6 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -24,6 +24,9 @@
   </properties>
   <body>
     <release version="2.6" date="2016-MM-DD" description="GA Release 2.6">
+      <action issue="LOG4J2-1357" dev="mikes" type="add">
+        Option to not log stacktraces for logged Throwables in GelfLayout.
+      </action>
       <action issue="LOG4J2-1375" dev="rpopma" type="update">
         Update SLF4J from 1.7.13 to 1.7.21.
       </action>

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d1a73753/src/site/xdoc/manual/layouts.xml.vm
----------------------------------------------------------------------
diff --git a/src/site/xdoc/manual/layouts.xml.vm b/src/site/xdoc/manual/layouts.xml.vm
index 83f2e57..e94e5e7 100644
--- a/src/site/xdoc/manual/layouts.xml.vm
+++ b/src/site/xdoc/manual/layouts.xml.vm
@@ -196,7 +196,7 @@ logger.debug("one={}, two={}, three={}", 1, 2, 3);
           <p>
             Configure as follows to send to a Graylog2 server:
           </p>
-          <pre class="prettyprint linenums">[
+          <pre class="prettyprint linenums">
   &lt;Appenders&gt;
     &lt;Socket name="Graylog" protocol="udp" host="graylog.domain.com" port="12201"&gt;
         &lt;GelfLayout host="someserver" compressionType="GZIP" compressionThreshold="1024"&gt;
@@ -205,13 +205,40 @@ logger.debug("one={}, two={}, three={}", 1, 2, 3);
         &lt;/GelfLayout&gt;
     &lt;/Socket&gt;
   &lt;/Appenders&gt;
-]
 </pre>
+          <table>
+            <tr>
+              <th>Parameter Name</th>
+              <th>Type</th>
+              <th>Description</th>
+            </tr>
+            <tr>
+              <td>host</td>
+              <td>String</td>
+              <td>The value of the <code>host</code> property (mandatory).</td>
+            </tr>
+            <tr>
+              <td>compressionType</td>
+              <td><code>GZIP</code>, <code>ZLIB</code> or <code>OFF</code></td>
+              <td>Compression to use (optional, defaults to <code>GZIP</code>)</td>
+            </tr>
+            <tr>
+              <td>compressionThreshold</td>
+              <td>int</td>
+              <td>compress if data is larger than this number of bytes (optional, defaults to 1024)</td>
+            </tr>
+            <tr>
+              <td>includeStacktrace</td>
+              <td>boolean</td>
+              <td>Whether to include full stacktrace of logged Throwables (optional, default to true)</td>
+            </tr>
+            <caption align="top">GELF Layout Parameters</caption>
+          </table>
            <p>
              See also:
            </p>
            <ul>
-             <li>The <a href="https://www.graylog.org/resources/gelf/">GELF specification</a></li>
+             <li>The <a href="http://docs.graylog.org/en/latest/pages/gelf.html#gelf">GELF specification</a></li>
            </ul>
         </subsection>
         <a name="HTMLLayout"/>