logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gary Gregory <garydgreg...@gmail.com>
Subject Fwd: svn commit: r1597229 - in /logging/log4j/log4j2/trunk/log4j-core/src: main/java/org/apache/logging/log4j/core/filter/ test/java/org/apache/logging/log4j/core/filter/
Date Sat, 24 May 2014 01:07:09 GMT
Cool beans! 

It's awesome to get rid of all this conversion boiler plate code.

Gary

<div>-------- Original message --------</div><div>From: mattsicker@apache.org
</div><div>Date:05/23/2014  20:50  (GMT-05:00) </div><div>To: commits@logging.apache.org
</div><div>Subject: svn commit: r1597229 - in /logging/log4j/log4j2/trunk/log4j-core/src:
 main/java/org/apache/logging/log4j/core/filter/
  test/java/org/apache/logging/log4j/core/filter/
</div><div>
</div>Author: mattsicker
Date: Sat May 24 00:50:30 2014
New Revision: 1597229

URL: http://svn.apache.org/r1597229
Log:
Migrate more plugins to use types other than String.

Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/MapFilter.java
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/MarkerFilter.java
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/RegexFilter.java
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/StructuredDataFilter.java
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/ThreadContextMapFilter.java
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/ThresholdFilter.java
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/TimeFilter.java
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/RegexFilterTest.java
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/ThresholdFilterTest.java

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/MapFilter.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/MapFilter.java?rev=1597229&r1=1597228&r2=1597229&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/MapFilter.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/MapFilter.java
Sat May 24 00:50:30 2014
@@ -119,8 +119,8 @@ public class MapFilter extends AbstractF
     public static MapFilter createFilter(
             @PluginElement("Pairs") final KeyValuePair[] pairs,
             @PluginAttribute("operator") final String oper,
-            @PluginAttribute("onMatch") final String match,
-            @PluginAttribute("onMismatch") final String mismatch) {
+            @PluginAttribute("onMatch") final Result match,
+            @PluginAttribute("onMismatch") final Result mismatch) {
         if (pairs == null || pairs.length == 0) {
             LOGGER.error("keys and values must be specified for the MapFilter");
             return null;
@@ -151,8 +151,6 @@ public class MapFilter extends AbstractF
             return null;
         }
         final boolean isAnd = oper == null || !oper.equalsIgnoreCase("or");
-        final Result onMatch = Result.toResult(match);
-        final Result onMismatch = Result.toResult(mismatch);
-        return new MapFilter(map, isAnd, onMatch, onMismatch);
+        return new MapFilter(map, isAnd, match, mismatch);
     }
}

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/MarkerFilter.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/MarkerFilter.java?rev=1597229&r1=1597228&r2=1597229&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/MarkerFilter.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/MarkerFilter.java
Sat May 24 00:50:30 2014
@@ -82,16 +82,14 @@ public final class MarkerFilter extends 
     @PluginFactory
     public static MarkerFilter createFilter(
             @PluginAttribute("marker") final String marker,
-            @PluginAttribute("onMatch") final String match,
-            @PluginAttribute("onMismatch") final String mismatch) {
+            @PluginAttribute("onMatch") final Result match,
+            @PluginAttribute("onMismatch") final Result mismatch) {

         if (marker == null) {
             LOGGER.error("A marker must be provided for MarkerFilter");
             return null;
         }
-        final Result onMatch = Result.toResult(match);
-        final Result onMismatch = Result.toResult(mismatch);
-        return new MarkerFilter(marker, onMatch, onMismatch);
+        return new MarkerFilter(marker, match, mismatch);
     }

}

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/RegexFilter.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/RegexFilter.java?rev=1597229&r1=1597228&r2=1597229&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/RegexFilter.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/RegexFilter.java
Sat May 24 00:50:30 2014
@@ -105,27 +105,16 @@ public final class RegexFilter extends A
      */
     @PluginFactory
     public static RegexFilter createFilter(
-            @PluginAttribute("regex") final String regex,
-            @PluginAttribute("useRawMsg") final String useRawMsg,
-            @PluginAttribute("onMatch") final String match,
-            @PluginAttribute("onMismatch") final String mismatch) {
+            @PluginAttribute("regex") final Pattern regex,
+            @PluginAttribute("useRawMsg") final Boolean useRawMsg,
+            @PluginAttribute("onMatch") final Result match,
+            @PluginAttribute("onMismatch") final Result mismatch) {

         if (regex == null) {
             LOGGER.error("A regular expression must be provided for RegexFilter");
             return null;
         }
-        final boolean raw = Boolean.parseBoolean(useRawMsg);
-        Pattern pattern;
-        try {
-            pattern = Pattern.compile(regex);
-        } catch (final Exception ex) {
-            LOGGER.error("RegexFilter caught exception compiling pattern: " + regex + " cause:
" + ex.getMessage());
-            return null;
-        }
-        final Result onMatch = Result.toResult(match);
-        final Result onMismatch = Result.toResult(mismatch);
-
-        return new RegexFilter(raw, pattern, onMatch, onMismatch);
+        return new RegexFilter(useRawMsg, regex, match, mismatch);
     }

}

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/StructuredDataFilter.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/StructuredDataFilter.java?rev=1597229&r1=1597228&r2=1597229&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/StructuredDataFilter.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/StructuredDataFilter.java
Sat May 24 00:50:30 2014
@@ -104,8 +104,8 @@ public final class StructuredDataFilter 
     public static StructuredDataFilter createFilter(
             @PluginElement("Pairs") final KeyValuePair[] pairs,
             @PluginAttribute("operator") final String oper,
-            @PluginAttribute("onMatch") final String match,
-            @PluginAttribute("onMismatch") final String mismatch) {
+            @PluginAttribute("onMatch") final Result match,
+            @PluginAttribute("onMismatch") final Result mismatch) {
         if (pairs == null || pairs.length == 0) {
             LOGGER.error("keys and values must be specified for the StructuredDataFilter");
             return null;
@@ -136,8 +136,6 @@ public final class StructuredDataFilter 
             return null;
         }
         final boolean isAnd = oper == null || !oper.equalsIgnoreCase("or");
-        final Result onMatch = Result.toResult(match);
-        final Result onMismatch = Result.toResult(mismatch);
-        return new StructuredDataFilter(map, isAnd, onMatch, onMismatch);
+        return new StructuredDataFilter(map, isAnd, match, mismatch);
     }
}

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/ThreadContextMapFilter.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/ThreadContextMapFilter.java?rev=1597229&r1=1597228&r2=1597229&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/ThreadContextMapFilter.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/ThreadContextMapFilter.java
Sat May 24 00:50:30 2014
@@ -114,8 +114,8 @@ public class ThreadContextMapFilter exte
     public static ThreadContextMapFilter createFilter(
             @PluginElement("Pairs") final KeyValuePair[] pairs,
             @PluginAttribute("operator") final String oper,
-            @PluginAttribute("onMatch") final String match,
-            @PluginAttribute("onMismatch") final String mismatch) {
+            @PluginAttribute("onMatch") final Result match,
+            @PluginAttribute("onMismatch") final Result mismatch) {
         if (pairs == null || pairs.length == 0) {
             LOGGER.error("key and value pairs must be specified for the ThreadContextMapFilter");
             return null;
@@ -146,8 +146,6 @@ public class ThreadContextMapFilter exte
             return null;
         }
         final boolean isAnd = oper == null || !oper.equalsIgnoreCase("or");
-        final Result onMatch = Result.toResult(match);
-        final Result onMismatch = Result.toResult(mismatch);
-        return new ThreadContextMapFilter(map, isAnd, onMatch, onMismatch);
+        return new ThreadContextMapFilter(map, isAnd, match, mismatch);
     }
}

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/ThresholdFilter.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/ThresholdFilter.java?rev=1597229&r1=1597228&r2=1597229&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/ThresholdFilter.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/ThresholdFilter.java
Sat May 24 00:50:30 2014
@@ -77,20 +77,20 @@ public final class ThresholdFilter exten

     /**
      * Create a ThresholdFilter.
-     * @param levelName The log Level.
+     * @param level The log Level.
      * @param match The action to take on a match.
      * @param mismatch The action to take on a mismatch.
      * @return The created ThresholdFilter.
      */
     @PluginFactory
     public static ThresholdFilter createFilter(
-            @PluginAttribute("level") final String levelName,
-            @PluginAttribute("onMatch") final String match,
-            @PluginAttribute("onMismatch") final String mismatch) {
-        final Level level = Level.toLevel(levelName, Level.ERROR);
-        final Result onMatch = Result.toResult(match, Result.NEUTRAL);
-        final Result onMismatch = Result.toResult(mismatch, Result.DENY);
-        return new ThresholdFilter(level, onMatch, onMismatch);
+            @PluginAttribute("level") final Level level,
+            @PluginAttribute("onMatch") final Result match,
+            @PluginAttribute("onMismatch") final Result mismatch) {
+        final Level actualLevel = level == null ? Level.ERROR : level;
+        final Result onMatch = match == null ? Result.NEUTRAL : match;
+        final Result onMismatch = mismatch == null ? Result.DENY : mismatch;
+        return new ThresholdFilter(actualLevel, onMatch, onMismatch);
     }

}

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/TimeFilter.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/TimeFilter.java?rev=1597229&r1=1597228&r2=1597229&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/TimeFilter.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/TimeFilter.java
Sat May 24 00:50:30 2014
@@ -106,8 +106,8 @@ public final class TimeFilter extends Ab
             @PluginAttribute("start") final String start,
             @PluginAttribute("end") final String end,
             @PluginAttribute("timezone") final String tz,
-            @PluginAttribute("onMatch") final String match,
-            @PluginAttribute("onMismatch") final String mismatch) {
+            @PluginAttribute("onMatch") final Result match,
+            @PluginAttribute("onMismatch") final Result mismatch) {
         final SimpleDateFormat stf = new SimpleDateFormat("HH:mm:ss");
         long s = 0;
         if (start != null) {
@@ -128,8 +128,8 @@ public final class TimeFilter extends Ab
             }
         }
         final TimeZone timezone = tz == null ? TimeZone.getDefault() : TimeZone.getTimeZone(tz);
-        final Result onMatch = Result.toResult(match, Result.NEUTRAL);
-        final Result onMismatch = Result.toResult(mismatch, Result.DENY);
+        final Result onMatch = match == null ? Result.NEUTRAL : match;
+        final Result onMismatch = mismatch == null ? Result.DENY : mismatch;
         return new TimeFilter(s, e, timezone, onMatch, onMismatch);
     }


Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/RegexFilterTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/RegexFilterTest.java?rev=1597229&r1=1597228&r2=1597229&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/RegexFilterTest.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/RegexFilterTest.java
Sat May 24 00:50:30 2014
@@ -16,9 +16,12 @@
  */
package org.apache.logging.log4j.core.filter;

+import java.util.regex.Pattern;
+
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.plugins.util.TypeConverters;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.SimpleMessage;
@@ -39,7 +42,7 @@ public class RegexFilterTest {

     @Test
     public void testThresholds() {
-        RegexFilter filter = RegexFilter.createFilter(".* test .*", null, null, null);
+        RegexFilter filter = RegexFilter.createFilter(Pattern.compile(".* test .*"), false,
null, null);
         filter.start();
         assertTrue(filter.isStarted());
         assertSame(Filter.Result.NEUTRAL,
@@ -49,13 +52,13 @@ public class RegexFilterTest {
         assertSame(Filter.Result.NEUTRAL, filter.filter(event));
         event = new Log4jLogEvent(null, null, null, Level.ERROR, new SimpleMessage("test"),
null);
         assertSame(Filter.Result.DENY, filter.filter(event));
-        filter = RegexFilter.createFilter("* test *", null, null, null);
+        filter = RegexFilter.createFilter((Pattern) TypeConverters.convert("* test *", Pattern.class),
false, null, null);
         assertNull(filter);
     }

     @Test
     public void TestNoMsg() {
-        final RegexFilter filter = RegexFilter.createFilter(".* test .*", null, null, null);
+        final RegexFilter filter = RegexFilter.createFilter(Pattern.compile(".* test .*"),
false, null, null);
         filter.start();
         assertTrue(filter.isStarted());
         assertSame(Filter.Result.DENY, filter.filter(null, Level.DEBUG, null, (String) null,
(Throwable) null));

Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/ThresholdFilterTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/ThresholdFilterTest.java?rev=1597229&r1=1597228&r2=1597229&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/ThresholdFilterTest.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/ThresholdFilterTest.java
Sat May 24 00:50:30 2014
@@ -32,7 +32,7 @@ public class ThresholdFilterTest {

     @Test
     public void testThresholds() {
-        final ThresholdFilter filter = ThresholdFilter.createFilter("ERROR", null, null);
+        final ThresholdFilter filter = ThresholdFilter.createFilter(Level.ERROR, null, null);
         filter.start();
         assertTrue(filter.isStarted());
         assertSame(Filter.Result.DENY, filter.filter(null, Level.DEBUG, null, null, (Throwable)
null));


Mime
View raw message