Repository: logging-log4j2 Updated Branches: refs/heads/LOG4J2-1169 [created] 9477c27fe
Add LOG4J2-1169 patch. Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/9477c27f Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/9477c27f Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/9477c27f Branch: refs/heads/LOG4J2-1169 Commit: 9477c27fea43d3eabf074b6ca231516c4ccba8c1 Parents: 7f64fe8 Author: Matt Sicker <[email protected]> Authored: Mon Nov 30 13:45:01 2015 -0600 Committer: Matt Sicker <[email protected]> Committed: Mon Nov 30 13:45:01 2015 -0600 ---------------------------------------------------------------------- .../pattern/EqualsReplacementConverter.java | 37 +++++++++++++- .../log4j/core/layout/PatternLayoutTest.java | 22 ++++++++ .../pattern/EqualsReplacementConverterTest.java | 53 ++++++++++++++++++-- 3 files changed, 106 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9477c27f/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EqualsReplacementConverter.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EqualsReplacementConverter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EqualsReplacementConverter.java index 2534b5d..172b7aa 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EqualsReplacementConverter.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EqualsReplacementConverter.java @@ -19,6 +19,7 @@ package org.apache.logging.log4j.core.pattern; import java.util.List; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.core.layout.PatternLayout; @@ -70,7 +71,7 @@ public final class EqualsReplacementConverter extends LogEventPatternConverter { /** * Construct the converter. - * + * * @param formatters * The PatternFormatters to generate the text to manipulate. * @param testString @@ -96,6 +97,38 @@ public final class EqualsReplacementConverter extends LogEventPatternConverter { formatter.format(event, buf); } final String string = buf.toString(); - toAppendTo.append(testString.equals(string) ? substitution : string); + + // if pattern equals test append substitution + if (testString.equals(string)) { + final String substitutionString = parseSubstitution(event); + toAppendTo.append(substitutionString); + } + // if pattern equals not test append pattern + else { + toAppendTo.append(string); + } + } + + /** + * Returns the parsed substitution test. + * + * @param event the current log event + * @return the parsed substitution test + */ + protected String parseSubstitution(final LogEvent event) { + StringBuilder substitutionBuffer = new StringBuilder(); + // check if substitution needs to be parsed + if (substitution.contains("%")) { + // parse substitution pattern + final Configuration config = LoggerContext.getContext().getConfiguration(); + final List<PatternFormatter> substitutionFormatters = PatternLayout.createPatternParser(config).parse( + substitution); + for (final PatternFormatter formatter : substitutionFormatters) { + formatter.format(event, substitutionBuffer); + } + } else { + substitutionBuffer.append(substitution); + } + return substitutionBuffer.toString(); } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9477c27f/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java index 53335d6..c9029d0 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java @@ -223,6 +223,28 @@ public class PatternLayoutTest { } @Test + public void testEqualsMarkerWithMessageSubstitution() throws Exception { + // replace "[]" with the empty string + final PatternLayout layout = PatternLayout.newBuilder().withPattern("[%logger]%equals{[%marker]}{[]}{[%msg]}") + .withConfiguration(ctx.getConfiguration()).build(); + // Not empty marker + final LogEvent event1 = Log4jLogEvent.newBuilder() // + .setLoggerName(this.getClass().getName()).setLoggerFqcn("org.apache.logging.log4j.core.Logger") // + .setLevel(Level.INFO) // + .setMarker(MarkerManager.getMarker("TestMarker")) + .setMessage(new SimpleMessage("Hello, world!")).build(); + final byte[] result1 = layout.toByteArray(event1); + assertEquals("[org.apache.logging.log4j.core.layout.PatternLayoutTest][TestMarker]", new String(result1)); + // empty marker + final LogEvent event2 = Log4jLogEvent.newBuilder() // + .setLoggerName(this.getClass().getName()).setLoggerFqcn("org.apache.logging.log4j.core.Logger") // + .setLevel(Level.INFO) + .setMessage(new SimpleMessage("Hello, world!")).build(); + final byte[] result2 = layout.toByteArray(event2); + assertEquals("[org.apache.logging.log4j.core.layout.PatternLayoutTest][Hello, world!]", new String(result2)); + } + + @Test public void testSpecialChars() throws Exception { final PatternLayout layout = PatternLayout.newBuilder().withPattern("\\\\%level\\t%msg\\n\\t%logger\\r\\n\\f") .withConfiguration(ctx.getConfiguration()).build(); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9477c27f/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/EqualsReplacementConverterTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/EqualsReplacementConverterTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/EqualsReplacementConverterTest.java index 5534662..6eb5315 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/EqualsReplacementConverterTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/EqualsReplacementConverterTest.java @@ -16,8 +16,6 @@ */ package org.apache.logging.log4j.core.pattern; -import static org.junit.Assert.assertEquals; - import org.apache.logging.log4j.Level; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.LoggerContext; @@ -26,11 +24,15 @@ import org.apache.logging.log4j.message.SimpleMessage; import org.apache.logging.log4j.util.Strings; import org.junit.Test; +import static org.junit.Assert.*; + /** * */ public class EqualsReplacementConverterTest { + private String testMessage = "This is a test"; + @Test public void testMarkerReplacement() { testReplacement("%marker", Strings.EMPTY); @@ -46,18 +48,61 @@ public class EqualsReplacementConverterTest { testReplacement("%logger", "[" + EqualsReplacementConverterTest.class.getName() + "]"); } + @Test + public void testMarkerReplacementWithMessage() { + testReplacement(testMessage, new String[]{"[%marker]", "[]", "%msg"}); + } + private void testReplacement(String tag, String expectedValue) { + final String[] options = new String[]{"[" + tag + "]", "[]", expectedValue}; + testReplacement(expectedValue, options); + } + + private void testReplacement(String expectedValue, String[] options) { final LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName(EqualsReplacementConverterTest.class.getName()) // .setLevel(Level.DEBUG) // - .setMessage(new SimpleMessage("This is a test")) // + .setMessage(new SimpleMessage(testMessage)) // .build(); final StringBuilder sb = new StringBuilder(); final LoggerContext ctx = LoggerContext.getContext(); - final String[] options = new String[] { "[" + tag + "]", "[]", expectedValue }; final EqualsReplacementConverter converter = EqualsReplacementConverter.newInstance(ctx.getConfiguration(), options); converter.format(event, sb); assertEquals(expectedValue, sb.toString()); } + + @Test + public void testParseSubstitutionWithPattern() { + testParseSubstitution("%msg", testMessage); + } + + @Test + public void testParseSubstitutionWithoutPattern() { + String substitution = "test"; + testParseSubstitution(substitution, substitution); + } + + @Test + public void testParseSubstitutionEmpty() { + testParseSubstitution("", ""); + } + + @Test + public void testParseSubstitutionWithWhiteSpaces() { + testParseSubstitution(" ", " "); + } + + private void testParseSubstitution(String substitution, String expected) { + final LogEvent event = Log4jLogEvent.newBuilder() + .setLoggerName(EqualsReplacementConverterTest.class.getName()) + .setLevel(Level.DEBUG) + .setMessage(new SimpleMessage(testMessage)) + .build(); + final LoggerContext ctx = LoggerContext.getContext(); + final EqualsReplacementConverter converter = EqualsReplacementConverter.newInstance(ctx.getConfiguration(), + new String[]{"[%marker]", "[]", substitution}); + final String actual = converter.parseSubstitution(event); + assertEquals(expected, actual); + } }
