logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1187648 - in /logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src: main/java/org/apache/logging/log4j/core/layout/ main/java/org/apache/logging/log4j/core/pattern/ test/java/org/apache/logging/log4j/core/pattern/ test/res...
Date Sat, 22 Oct 2011 04:18:03 GMT
Author: rgoers
Date: Sat Oct 22 04:18:03 2011
New Revision: 1187648

URL: http://svn.apache.org/viewvc?rev=1187648&view=rev
Log:
Add RegexReplacementPatternConverter

Added:
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/RegexReplacementConverter.java
      - copied, changed from r1185987, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/RegexReplacement.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/RegexReplacementConverterTest.java
      - copied, changed from r1186305, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/MarkerPatternConverterTest.java
Modified:
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/RegexReplacementTest.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/resources/log4j-replace.xml

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java?rev=1187648&r1=1187647&r2=1187648&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java
Sat Oct 22 04:18:03 2011
@@ -516,7 +516,7 @@ public class PatternLayout extends Abstr
         return config == null ? str : config.getSubst().replace(event, str);
     }
 
-    private PatternParser createPatternParser(Configuration config) {
+    public static PatternParser createPatternParser(Configuration config) {
         if (config == null) {
             return new PatternParser(config, KEY, LogEventPatternConverter.class);
         }

Copied: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/RegexReplacementConverter.java
(from r1185987, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/RegexReplacement.java)
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/RegexReplacementConverter.java?p2=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/RegexReplacementConverter.java&p1=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/RegexReplacement.java&r1=1185987&r2=1187648&rev=1187648&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/RegexReplacement.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/RegexReplacementConverter.java
Sat Oct 22 04:18:03 2011
@@ -22,7 +22,6 @@ import org.apache.logging.log4j.core.Log
 import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.plugins.Plugin;
 import org.apache.logging.log4j.core.config.plugins.PluginAttr;
-import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
 import org.apache.logging.log4j.core.layout.PatternLayout;
 import org.apache.logging.log4j.status.StatusLogger;
@@ -31,10 +30,11 @@ import java.util.List;
 import java.util.regex.Pattern;
 
 /**
- *
+ * Replacement pattern converter.
  */
-@Plugin(name="replace", type="Core", printObject=true)
-public final class RegexReplacement {
+@Plugin(name="replace", type="Converter")
+@ConverterKeys({"replace"})
+public final class RegexReplacementConverter extends LogEventPatternConverter {
 
     private final Pattern pattern;
 
@@ -42,39 +42,62 @@ public final class RegexReplacement {
 
     private static Logger logger = StatusLogger.getLogger();
 
+    private List<PatternConverter> converters;
+
     /**
-     * Private constructor.
-     *
-     * @param options options, may be null.
+     * Construct the converter.
+     * @param converters The PatternConverters to generate the text to manipulate.
+     * @param pattern The regular expression Pattern.
+     * @param substitution The substitution string.
      */
-    private RegexReplacement(Pattern pattern, String substitution) {
+    private RegexReplacementConverter(List<PatternConverter> converters,
+                                      Pattern pattern, String substitution) {
+        super("replace", "replace");
         this.pattern = pattern;
         this.substitution = substitution;
+        this.converters = converters;
     }
 
     /**
-     * {@inheritDoc}
+     * Gets an instance of the class.
+     *
+     * @param config The current Configuration.
+     * @param options pattern options, may be null.  If first element is "short",
+     *                only the first line of the throwable will be formatted.
+     * @return instance of class.
      */
-    public String format(String msg) {
-        return pattern.matcher(msg).replaceAll(substitution);
-    }
-
-    public String toString() {
-        return "replace(regex=" + pattern.pattern() + ", replacement=" + substitution + ")";
-    }
-
-    @PluginFactory
-    public static RegexReplacement createRegexReplacement(@PluginAttr("regex") String regex,
-                                                          @PluginAttr("replacement") String
replacement) {
-        if (regex == null) {
-            logger.error("A regular expression is required for replacement");
+    public static RegexReplacementConverter newInstance(Configuration config, final String[]
options) {
+        if (options.length != 3) {
+            logger.error("Incorrect number of options on replace. Expected 3 received " +
options.length);
             return null;
         }
-        if (replacement == null) {
-            logger.error("A replacement string is required to perform replacement");
+        if (options[0] == null) {
+            logger.error("No pattern supplied on replace");
+            return null;
         }
-        Pattern p = Pattern.compile(regex);
-        return new RegexReplacement(p, replacement);
+        if (options[1] == null) {
+            logger.error("No regular expression supplied on replace");
+            return null;
+        }
+        if (options[2] == null) {
+            logger.error("No substitution supplied on replace");
+            return null;
+        }
+        Pattern p = Pattern.compile(options[1]);
+        PatternParser parser = PatternLayout.createPatternParser(config);
+        List<PatternConverter> converters = parser.parse(options[0]);
+        return new RegexReplacementConverter(converters, p, options[2]);
     }
 
+
+    /**
+     * {@inheritDoc}
+     */
+    public void format(final LogEvent event, final StringBuilder toAppendTo) {
+        StringBuilder buf = new StringBuilder();
+        for (PatternConverter c : converters) {
+            c.format(event, buf);
+        }
+        toAppendTo.append(pattern.matcher(buf.toString()).replaceAll(substitution));
+    }
 }

Copied: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/RegexReplacementConverterTest.java
(from r1186305, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/MarkerPatternConverterTest.java)
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/RegexReplacementConverterTest.java?p2=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/RegexReplacementConverterTest.java&p1=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/MarkerPatternConverterTest.java&r1=1186305&r2=1187648&rev=1187648&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/MarkerPatternConverterTest.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/RegexReplacementConverterTest.java
Sat Oct 22 04:18:03 2011
@@ -17,32 +17,40 @@
 package org.apache.logging.log4j.core.pattern;
 
 import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Marker;
 import org.apache.logging.log4j.MarkerManager;
+import org.apache.logging.log4j.ThreadContext;
 import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.LoggerContext;
 import org.apache.logging.log4j.core.impl.Log4jLogEvent;
 import org.apache.logging.log4j.message.Message;
+import org.apache.logging.log4j.message.SimpleMessage;
 import org.apache.logging.log4j.message.StructuredDataMessage;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
 
 /**
  *
  */
-public class MarkerPatternConverterTest {
+public class RegexReplacementConverterTest {
 
 
     @Test
-    public void testLookup() {
-        Message msg = new StructuredDataMessage("Test", "This is a test", "Audit");
-        Marker eventMarker = MarkerManager.getMarker("EVENT");
-        Marker auditMarker = MarkerManager.getMarker("AUDIT", eventMarker);
-        LogEvent event = new Log4jLogEvent("MyLogger", auditMarker, null, Level.DEBUG, msg,
null);
+    public void testReplacement() {
+        ThreadContext.put("MyKey", "Apache");
+        LogEvent event = new Log4jLogEvent(RegexReplacementConverterTest.class.getName(),
null, null,
+            Level.DEBUG, new SimpleMessage("This is a test"), null);
         StringBuilder sb = new StringBuilder();
-        MarkerPatternConverter converter = MarkerPatternConverter.newInstance(null);
+        LoggerContext ctx = (LoggerContext) LogManager.getContext();
+        String[] options = new String[] {
+            "%logger %msg%n", "\\.", "/"
+        };
+        RegexReplacementConverter converter = RegexReplacementConverter.newInstance(ctx.getConfiguration(),
+            options);
         converter.format(event, sb);
-        assertEquals(auditMarker.toString(), sb.toString());
+        assertEquals("org/apache/logging/log4j/core/pattern/RegexReplacementConverterTest
This is a test\n",
+            sb.toString());
     }
 }

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/RegexReplacementTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/RegexReplacementTest.java?rev=1187648&r1=1187647&r2=1187648&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/RegexReplacementTest.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/RegexReplacementTest.java
Sat Oct 22 04:18:03 2011
@@ -44,6 +44,7 @@ public class RegexReplacementTest {
     private static final String CONFIG = "log4j-replace.xml";
     private static Configuration config;
     private static ListAppender app;
+    private static ListAppender app2;
     private static LoggerContext ctx;
 
     @BeforeClass
@@ -54,7 +55,9 @@ public class RegexReplacementTest {
         for (Map.Entry<String, Appender> entry : config.getAppenders().entrySet())
{
             if (entry.getKey().equals("List")) {
                 app = (ListAppender) entry.getValue();
-                break;
+            }
+            if (entry.getKey().equals("List2")) {
+                app2 = (ListAppender) entry.getValue();
             }
         }
     }
@@ -68,6 +71,7 @@ public class RegexReplacementTest {
     }
 
     org.apache.logging.log4j.Logger logger = LogManager.getLogger("LoggerTest");
+    org.apache.logging.log4j.Logger logger2 = LogManager.getLogger("ReplacementTest");
 
     @Test
     public void testReplacement() {
@@ -83,5 +87,16 @@ public class RegexReplacementTest {
         assertNotNull(msgs);
         assertTrue("Incorrect number of messages. Should be 1 is " + msgs.size(), msgs.size()
== 1);
         assertEquals("LoggerTest This is a test for Apache\n", msgs.get(0));
+        app.clear();
+
+    }
+     @Test
+    public void testConverter() {
+        logger2.error(this.getClass().getName());
+        List<String> msgs = app2.getMessages();
+        assertNotNull(msgs);
+        assertTrue("Incorrect number of messages. Should be 1 is " + msgs.size(), msgs.size()
== 1);
+        assertTrue("Replacement failed", msgs.get(0).endsWith("/RegexReplacementTest\n"));
+        app2.clear();
     }
 }

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/resources/log4j-replace.xml
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/resources/log4j-replace.xml?rev=1187648&r1=1187647&r2=1187648&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/resources/log4j-replace.xml
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/resources/log4j-replace.xml
Sat Oct 22 04:18:03 2011
@@ -7,9 +7,17 @@
          <pattern>%logger %msg%n</pattern>
       </PatternLayout>
     </List>
+    <List name="List2">
+       <PatternLayout>
+         <pattern>%replace{%logger %msg%n}{\.}{/}</pattern>
+      </PatternLayout>
+    </List>
   </Appenders>
 
   <loggers>
+    <logger level="trace" name="ReplacementTest" additivity="false">
+      <appender-ref ref="List2"/>
+    </logger>
     <root level="trace">
       <appender-ref ref="List"/>
     </root>



Mime
View raw message