You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2020/08/16 04:46:50 UTC
[logging-log4j2] branch release-2.x updated: LOG4J2-2892 - Allow
GelfLayout to produce newline delimited events
This is an automated email from the ASF dual-hosted git repository.
rgoers pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/release-2.x by this push:
new 8541463 LOG4J2-2892 - Allow GelfLayout to produce newline delimited events
8541463 is described below
commit 8541463f10ebf77924a30bd6e60c950df77155af
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Sat Aug 15 21:46:29 2020 -0700
LOG4J2-2892 - Allow GelfLayout to produce newline delimited events
---
.../logging/log4j/core/layout/GelfLayout.java | 36 ++++++++++++++++++----
.../logging/log4j/core/layout/GelfLayoutTest.java | 31 ++++++++++++++-----
src/changes/changes.xml | 3 ++
3 files changed, 56 insertions(+), 14 deletions(-)
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 5651d37..0739b60 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
@@ -105,6 +105,7 @@ public final class GelfLayout extends AbstractStringLayout {
private final boolean includeStacktrace;
private final boolean includeThreadContext;
private final boolean includeNullDelimiter;
+ private final boolean includeNewLineDelimiter;
private final PatternLayout layout;
private final FieldWriter fieldWriter;
@@ -133,6 +134,9 @@ public final class GelfLayout extends AbstractStringLayout {
private boolean includeNullDelimiter = false;
@PluginBuilderAttribute
+ private boolean includeNewLineDelimiter = false;
+
+ @PluginBuilderAttribute
private String threadContextIncludes = null;
@PluginBuilderAttribute
@@ -181,7 +185,8 @@ public final class GelfLayout extends AbstractStringLayout {
.build();
}
return new GelfLayout(getConfiguration(), host, additionalFields, compressionType, compressionThreshold,
- includeStacktrace, includeThreadContext, includeNullDelimiter, checker, patternLayout);
+ includeStacktrace, includeThreadContext, includeNullDelimiter, includeNewLineDelimiter, checker,
+ patternLayout);
}
public String getHost() {
@@ -206,6 +211,10 @@ public final class GelfLayout extends AbstractStringLayout {
public boolean isIncludeNullDelimiter() { return includeNullDelimiter; }
+ public boolean isIncludeNewLineDelimiter() {
+ return includeNewLineDelimiter;
+ }
+
public KeyValuePair[] getAdditionalFields() {
return additionalFields;
}
@@ -273,6 +282,16 @@ public final class GelfLayout extends AbstractStringLayout {
}
/**
+ * Whether to include newline (LF) as delimiter after each event (optional, default to false).
+ *
+ * @return this builder
+ */
+ public B setIncludeNewLineDelimiter(final boolean includeNewLineDelimiter) {
+ this.includeNewLineDelimiter = includeNewLineDelimiter;
+ return asBuilder();
+ }
+
+ /**
* Additional fields to set on each log event.
*
* @return this builder
@@ -319,14 +338,14 @@ public final class GelfLayout extends AbstractStringLayout {
@Deprecated
public GelfLayout(final String host, final KeyValuePair[] additionalFields, final CompressionType compressionType,
final int compressionThreshold, final boolean includeStacktrace) {
- this(null, host, additionalFields, compressionType, compressionThreshold, includeStacktrace, true, false, null,
- null);
+ this(null, host, additionalFields, compressionType, compressionThreshold, includeStacktrace, true, false, false,
+ null, null);
}
private GelfLayout(final Configuration config, final String host, final KeyValuePair[] additionalFields,
final CompressionType compressionType, final int compressionThreshold, final boolean includeStacktrace,
- final boolean includeThreadContext, final boolean includeNullDelimiter, final ListChecker listChecker,
- final PatternLayout patternLayout) {
+ final boolean includeThreadContext, final boolean includeNullDelimiter, final boolean includeNewLineDelimiter,
+ final ListChecker listChecker, final PatternLayout patternLayout) {
super(config, StandardCharsets.UTF_8, null, null);
this.host = host != null ? host : NetUtils.getLocalHostname();
this.additionalFields = additionalFields != null ? additionalFields : new KeyValuePair[0];
@@ -342,6 +361,7 @@ public final class GelfLayout extends AbstractStringLayout {
this.includeStacktrace = includeStacktrace;
this.includeThreadContext = includeThreadContext;
this.includeNullDelimiter = includeNullDelimiter;
+ this.includeNewLineDelimiter = includeNewLineDelimiter;
if (includeNullDelimiter && compressionType != CompressionType.OFF) {
throw new IllegalArgumentException("null delimiter cannot be used with compression");
}
@@ -358,6 +378,7 @@ public final class GelfLayout extends AbstractStringLayout {
sb.append(", includeStackTrace=").append(includeStacktrace);
sb.append(", includeThreadContext=").append(includeThreadContext);
sb.append(", includeNullDelimiter=").append(includeNullDelimiter);
+ sb.append(", includeNewLineDelimiter=").append(includeNewLineDelimiter);
String threadVars = fieldWriter.getChecker().toString();
if (threadVars.length() > 0) {
sb.append(", ").append(threadVars);
@@ -384,7 +405,7 @@ public final class GelfLayout extends AbstractStringLayout {
defaultBoolean = true) final boolean includeStacktrace) {
// @formatter:on
return new GelfLayout(null, host, additionalFields, compressionType, compressionThreshold, includeStacktrace,
- true, false, null, null);
+ true, false, false, null, null);
}
@PluginBuilderFactory
@@ -519,6 +540,9 @@ public final class GelfLayout extends AbstractStringLayout {
if (includeNullDelimiter) {
builder.append('\0');
}
+ if (includeNewLineDelimiter) {
+ builder.append('\n');
+ }
return builder;
}
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 15bdf39..3cc0a3a 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
@@ -84,7 +84,8 @@ public class GelfLayoutTest {
Logger root = ctx.getRootLogger();
private void testCompressedLayout(final CompressionType compressionType, final boolean includeStacktrace,
- final boolean includeThreadContext, String host, final boolean includeNullDelimiter) throws IOException {
+ final boolean includeThreadContext, String host, final boolean includeNullDelimiter,
+ final boolean includeNewLineDelimiter) throws IOException {
for (final Appender appender : root.getAppenders().values()) {
root.removeAppender(appender);
}
@@ -100,6 +101,7 @@ public class GelfLayoutTest {
.setIncludeStacktrace(includeStacktrace)
.setIncludeThreadContext(includeThreadContext)
.setIncludeNullDelimiter(includeNullDelimiter)
+ .setIncludeNewLineDelimiter(includeNewLineDelimiter)
.build();
final ListAppender eventAppender = new ListAppender("Events", null, null, true, false);
final ListAppender rawAppender = new ListAppender("Raw", null, layout, true, true);
@@ -222,41 +224,54 @@ public class GelfLayoutTest {
//@formatter:on
assertJsonEquals(expected, uncompressedString);
assertJsonEquals(expected, uncompressedString2);
+ if (includeNullDelimiter) {
+ assertEquals(uncompressedString.indexOf('\0'), uncompressedString.length() - 1);
+ assertEquals(uncompressedString2.indexOf('\0'), uncompressedString2.length() - 1);
+ }
+ if (includeNewLineDelimiter) {
+ assertEquals(uncompressedString.indexOf('\n'), uncompressedString.length() - 1);
+ assertEquals(uncompressedString2.indexOf('\n'), uncompressedString2.length() - 1);
+ }
}
@Test
public void testLayoutGzipCompression() throws Exception {
- testCompressedLayout(CompressionType.GZIP, true, true, HOSTNAME, false);
+ testCompressedLayout(CompressionType.GZIP, true, true, HOSTNAME, false, false);
}
@Test
public void testLayoutNoCompression() throws Exception {
- testCompressedLayout(CompressionType.OFF, true, true, HOSTNAME, false);
+ testCompressedLayout(CompressionType.OFF, true, true, HOSTNAME, false, false);
}
@Test
public void testLayoutZlibCompression() throws Exception {
- testCompressedLayout(CompressionType.ZLIB, true, true, HOSTNAME, false);
+ testCompressedLayout(CompressionType.ZLIB, true, true, HOSTNAME, false, false);
}
@Test
public void testLayoutNoStacktrace() throws Exception {
- testCompressedLayout(CompressionType.OFF, false, true, HOSTNAME, false);
+ testCompressedLayout(CompressionType.OFF, false, true, HOSTNAME, false, false);
}
@Test
public void testLayoutNoThreadContext() throws Exception {
- testCompressedLayout(CompressionType.OFF, true, false, HOSTNAME, false);
+ testCompressedLayout(CompressionType.OFF, true, false, HOSTNAME, false, false);
}
@Test
public void testLayoutNoHost() throws Exception {
- testCompressedLayout(CompressionType.OFF, true, true, null, false);
+ testCompressedLayout(CompressionType.OFF, true, true, null, false, false);
}
@Test
public void testLayoutNullDelimiter() throws Exception {
- testCompressedLayout(CompressionType.OFF, false, true, HOSTNAME, true);
+ testCompressedLayout(CompressionType.OFF, false, true, HOSTNAME, true, false);
+ }
+
+ @Test
+ public void testLayoutNewLineDelimiter() throws Exception {
+ testCompressedLayout(CompressionType.OFF, true, true, HOSTNAME, false, true);
}
@Test
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index e2119da..3c33ef7 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -30,6 +30,9 @@
- "remove" - Removed
-->
<release version="2.14.0" date="2020-MM-DD" description="GA Release 2.14.0">
+ <action issue="LOG4J2-2892" dev="rgoers" type="update" due-to="Jakub Lukes">
+ Allow GelfLayout to produce newline delimited events.
+ </action>
<action issue="LOG4J2-2906" dev="rgoers" type="fix" due-to="Stephen Joyner">
Fix UnsupportedOperationException when initializing the Log4j2CloudConfigLoggingSystem.
</action>