This is an automated email from the ASF dual-hosted git repository. rgoers pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 964dd4bbeb2b8d349623d3c073949c49c39cee64 Author: jiangtangbi <[email protected]> AuthorDate: Mon Apr 20 13:47:16 2020 +0800 Support location info in GelfLayout --- .../logging/log4j/core/layout/GelfLayout.java | 6 ++ .../logging/log4j/core/layout/GelfLayoutTest.java | 102 +++++++++++++-------- 2 files changed, 71 insertions(+), 37 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 268ec6a..dca3ff8 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 @@ -50,6 +50,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.zip.DeflaterOutputStream; import java.util.zip.GZIPOutputStream; @@ -388,6 +389,11 @@ public final class GelfLayout extends AbstractStringLayout { helper.encode(text, destination); } + @Override + public boolean requiresLocation() { + return Objects.nonNull(layout) && layout.requiresLocation(); + } + private byte[] compress(final byte[] bytes) { try { final ByteArrayOutputStream baos = new ByteArrayOutputStream(compressionThreshold / 8); 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 095bec6..15bdf39 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 @@ -20,7 +20,11 @@ import com.fasterxml.jackson.core.io.JsonStringEncoder; import org.apache.commons.io.IOUtils; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.ThreadContext; -import org.apache.logging.log4j.core.*; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.BasicConfigurationFactory; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.Logger; +import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.config.ConfigurationFactory; import org.apache.logging.log4j.core.layout.GelfLayout.CompressionType; import org.apache.logging.log4j.core.lookup.JavaLookup; @@ -45,9 +49,9 @@ import static net.javacrumbs.jsonunit.JsonAssert.assertJsonEquals; import static org.junit.Assert.assertEquals; public class GelfLayoutTest { - + static ConfigurationFactory configFactory = new BasicConfigurationFactory(); - + private static final String HOSTNAME = "TheHost"; private static final String KEY1 = "Key1"; private static final String KEY2 = "Key2"; @@ -61,7 +65,7 @@ public class GelfLayoutTest { private static final String VALUE1 = "Value1"; @Rule - public final ThreadContextRule threadContextRule = new ThreadContextRule(); + public final ThreadContextRule threadContextRule = new ThreadContextRule(); @AfterClass public static void cleanupClass() { @@ -86,17 +90,17 @@ public class GelfLayoutTest { } // set up appenders final GelfLayout layout = GelfLayout.newBuilder() - .setConfiguration(ctx.getConfiguration()) - .setHost(host) - .setAdditionalFields(new KeyValuePair[] { - new KeyValuePair(KEY1, VALUE1), - new KeyValuePair(KEY2, "${java:runtime}"), }) - .setCompressionType(compressionType) - .setCompressionThreshold(1024) - .setIncludeStacktrace(includeStacktrace) - .setIncludeThreadContext(includeThreadContext) - .setIncludeNullDelimiter(includeNullDelimiter) - .build(); + .setConfiguration(ctx.getConfiguration()) + .setHost(host) + .setAdditionalFields(new KeyValuePair[]{ + new KeyValuePair(KEY1, VALUE1), + new KeyValuePair(KEY2, "${java:runtime}"),}) + .setCompressionType(compressionType) + .setCompressionThreshold(1024) + .setIncludeStacktrace(includeStacktrace) + .setIncludeThreadContext(includeThreadContext) + .setIncludeNullDelimiter(includeNullDelimiter) + .build(); 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); @@ -159,11 +163,11 @@ public class GelfLayoutTest { "\"_thread\": \"" + threadName + "\"," + "\"_logger\": \"\"," + "\"short_message\": \"" + LINE2 + "\"," + - (includeThreadContext ? - "\"_" + MDCKEY1 + "\": \"" + MDCVALUE1 + "\"," + - "\"_" + MDCKEY2 + "\": \"" + MDCVALUE2 + "\"," - : - "") + + (includeThreadContext ? + "\"_" + MDCKEY1 + "\": \"" + MDCVALUE1 + "\"," + + "\"_" + MDCKEY2 + "\": \"" + MDCVALUE2 + "\"," + : + "") + "\"_" + KEY1 + "\": \"" + VALUE1 + "\"," + "\"_" + KEY2 + "\": \"" + javaLookup.getRuntime() + "\"" + "}", @@ -176,20 +180,20 @@ public class GelfLayoutTest { InputStream inflaterStream; InputStream inflaterStream2; switch (compressionType) { - case GZIP: - inflaterStream = new GZIPInputStream(bais); - inflaterStream2 = new GZIPInputStream(bais2); - break; - case ZLIB: - inflaterStream = new InflaterInputStream(bais); - inflaterStream2 = new InflaterInputStream(bais2); - break; - case OFF: - inflaterStream = bais; - inflaterStream2 = bais2; - break; - default: - throw new IllegalStateException("Missing test case clause"); + case GZIP: + inflaterStream = new GZIPInputStream(bais); + inflaterStream2 = new GZIPInputStream(bais2); + break; + case ZLIB: + inflaterStream = new InflaterInputStream(bais); + inflaterStream2 = new InflaterInputStream(bais2); + break; + case OFF: + inflaterStream = bais; + inflaterStream2 = bais2; + break; + default: + throw new IllegalStateException("Missing test case clause"); } final byte[] uncompressed = IOUtils.toByteArray(inflaterStream); final byte[] uncompressed2 = IOUtils.toByteArray(inflaterStream2); @@ -209,9 +213,9 @@ public class GelfLayoutTest { "\"full_message\": \"" + String.valueOf(JsonStringEncoder.getInstance().quoteAsString( includeStacktrace ? GelfLayout.formatThrowable(exception).toString() : exception.toString())) + "\"," + (includeThreadContext ? - "\"_" + MDCKEY1 + "\": \"" + MDCVALUE1 + "\"," + - "\"_" + MDCKEY2 + "\": \"" + MDCVALUE2 + "\"," - : "") + + "\"_" + MDCKEY1 + "\": \"" + MDCVALUE1 + "\"," + + "\"_" + MDCKEY2 + "\": \"" + MDCVALUE2 + "\"," + : "") + "\"_" + KEY1 + "\": \"" + VALUE1 + "\"," + "\"_" + KEY2 + "\": \"" + javaLookup.getRuntime() + "\"" + "}"; @@ -265,4 +269,28 @@ public class GelfLayoutTest { assertEquals("1458741206.653", GelfLayout.formatTimestamp(1458741206653L).toString()); assertEquals("9223372036854775.807", GelfLayout.formatTimestamp(Long.MAX_VALUE).toString()); } + + private void testRequiresLocation(String messagePattern, Boolean requiresLocation) { + + GelfLayout layout = GelfLayout.newBuilder() + .setMessagePattern(messagePattern) + .build(); + + assertEquals(layout.requiresLocation(), requiresLocation); + } + + @Test + public void testRequiresLocationPatternNotSet() { + testRequiresLocation(null, false); + } + + @Test + public void testRequiresLocationPatternNotContainsLocation() { + testRequiresLocation("%m %n", false); + } + + @Test + public void testRequiresLocationPatternContainsLocation() { + testRequiresLocation("%C %m %t", true); + } }
