You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by sd...@apache.org on 2009/05/20 18:15:24 UTC

svn commit: r776752 - in /logging/log4j/companions: extras/trunk/src/main/java/org/apache/log4j/rule/ receivers/trunk/src/main/java/org/apache/log4j/helpers/

Author: sdeboy
Date: Wed May 20 16:15:24 2009
New Revision: 776752

URL: http://svn.apache.org/viewvc?rev=776752&view=rev
Log:
Fix for Bugzilla issue 47209: Color Filter rule level==info broken

https://issues.apache.org/bugzilla/show_bug.cgi?id=47209

Changed util.logging levels so the INFO levels int values are the same.  Now, level inequality can be calculated based on the integer value of the level instead of .equals

Added a NotLevelEqualsRule so the same issue is resolved for the not equals case

Added:
    logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/rule/NotLevelEqualsRule.java
Modified:
    logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/rule/LevelEqualsRule.java
    logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/rule/NotEqualsRule.java
    logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/helpers/UtilLoggingLevel.java

Modified: logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/rule/LevelEqualsRule.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/rule/LevelEqualsRule.java?rev=776752&r1=776751&r2=776752&view=diff
==============================================================================
--- logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/rule/LevelEqualsRule.java (original)
+++ logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/rule/LevelEqualsRule.java Wed May 20 16:15:24 2009
@@ -96,7 +96,9 @@
      * {@inheritDoc}
      */
     public boolean evaluate(final LoggingEvent event) {
-        return level.equals(event.getLevel());
+        //both util.logging and log4j contain 'info' - use the int values instead of equality
+        //info level set to the same value for both levels
+        return level.toInt() == event.getLevel().toInt();
     }
 
     /**

Modified: logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/rule/NotEqualsRule.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/rule/NotEqualsRule.java?rev=776752&r1=776751&r2=776752&view=diff
==============================================================================
--- logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/rule/NotEqualsRule.java (original)
+++ logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/rule/NotEqualsRule.java Wed May 20 16:15:24 2009
@@ -17,11 +17,11 @@
 
 package org.apache.log4j.rule;
 
+import java.util.Stack;
+
 import org.apache.log4j.spi.LoggingEvent;
 import org.apache.log4j.spi.LoggingEventFieldResolver;
 
-import java.util.Stack;
-
 
 /**
  * A Rule class implementing not equals against two strings.
@@ -70,7 +70,11 @@
      * @return new instance.
      */
   public static Rule getRule(final String field, final String value) {
-    return new NotEqualsRule(field, value);
+    if (field.equalsIgnoreCase(LoggingEventFieldResolver.LEVEL_FIELD)) {
+        return NotLevelEqualsRule.getRule(value);
+    } else {
+        return new NotEqualsRule(field, value);
+    }
   }
 
     /**
@@ -88,7 +92,11 @@
     String p2 = stack.pop().toString();
     String p1 = stack.pop().toString();
 
-    return new NotEqualsRule(p1, p2);
+    if (p1.equalsIgnoreCase(LoggingEventFieldResolver.LEVEL_FIELD)) {
+        return NotLevelEqualsRule.getRule(p2);
+    } else {
+        return new NotEqualsRule(p1, p2);
+    }
   }
 
     /** {@inheritDoc} */

Added: logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/rule/NotLevelEqualsRule.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/rule/NotLevelEqualsRule.java?rev=776752&view=auto
==============================================================================
--- logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/rule/NotLevelEqualsRule.java (added)
+++ logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/rule/NotLevelEqualsRule.java Wed May 20 16:15:24 2009
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.log4j.rule;
+
+import java.io.IOException;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.helpers.UtilLoggingLevel;
+import org.apache.log4j.spi.LoggingEvent;
+
+/**
+ * A Rule class implementing not equals against two levels.
+ *
+ * @author Scott Deboy (sdeboy@apache.org)
+ */
+public class NotLevelEqualsRule extends AbstractRule {
+    /**
+     * Serialization ID.
+     */
+    static final long serialVersionUID = -3638386582899583994L;
+
+    /**
+     * Level.
+     */
+    private transient Level level;
+
+    /**
+     * List of levels.
+     */
+    private static List levelList = new LinkedList();
+
+    static {
+        populateLevels();
+    }
+
+    /**
+     * Create new instance.
+     * @param level level.
+     */
+    private NotLevelEqualsRule(final Level level) {
+        super();
+        this.level = level;
+    }
+
+    /**
+     * Populate list of levels.
+     */
+    private static void populateLevels() {
+        levelList = new LinkedList();
+
+        levelList.add(Level.FATAL.toString());
+        levelList.add(Level.ERROR.toString());
+        levelList.add(Level.WARN.toString());
+        levelList.add(Level.INFO.toString());
+        levelList.add(Level.DEBUG.toString());
+		Level trace = Level.toLevel(5000, null);
+		if (trace != null) {
+			levelList.add(trace.toString());
+	    }
+    }
+
+    /**
+     * Create new rule.
+     * @param value name of level.
+     * @return instance of NotLevelEqualsRule.
+     */
+    public static Rule getRule(final String value) {
+        Level thisLevel;
+        if (levelList.contains(value.toUpperCase())) {
+            thisLevel = Level.toLevel(value.toUpperCase());
+          } else {
+            thisLevel = UtilLoggingLevel.toLevel(value.toUpperCase());
+        }
+
+        return new NotLevelEqualsRule(thisLevel);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean evaluate(final LoggingEvent event) {
+        //both util.logging and log4j contain 'info' - use the int values instead of equality
+        //info level set to the same value for both levels
+        return level.toInt() != event.getLevel().toInt();
+    }
+
+    /**
+     * Deserialize the state of the object.
+     *
+     * @param in object input stream.
+     *
+     * @throws IOException if error in reading stream for deserialization.
+     */
+    private void readObject(final java.io.ObjectInputStream in)
+            throws IOException {
+        populateLevels();
+        boolean isUtilLogging = in.readBoolean();
+        int levelInt = in.readInt();
+        if (isUtilLogging) {
+            level = UtilLoggingLevel.toLevel(levelInt);
+        } else {
+            level = Level.toLevel(levelInt);
+        }
+    }
+
+    /**
+     * Serialize the state of the object.
+     *
+     * @param out object output stream.
+     *
+     * @throws IOException if error in writing stream during serialization.
+     */
+    private void writeObject(final java.io.ObjectOutputStream out)
+            throws IOException {
+        out.writeBoolean(level instanceof UtilLoggingLevel);
+        out.writeInt(level.toInt());
+    }
+}

Modified: logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/helpers/UtilLoggingLevel.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/helpers/UtilLoggingLevel.java?rev=776752&r1=776751&r2=776752&view=diff
==============================================================================
--- logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/helpers/UtilLoggingLevel.java (original)
+++ logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/helpers/UtilLoggingLevel.java Wed May 20 16:15:24 2009
@@ -40,15 +40,14 @@
     /**
      * Numerical value for SEVERE.
      */
-    public static final int SEVERE_INT = 17000;
+    public static final int SEVERE_INT = 22000;
     /**
      * Numerical value for WARNING.
      */
-    public static final int WARNING_INT = 16000;
-    /**
-     * Numerical value for INFO.
-     */
-    public static final int INFO_INT = 15000;
+    public static final int WARNING_INT = 21000;
+
+    //INFO level defined in parent as 20000..no need to redefine here
+    
     /**
      * Numerical value for CONFIG.
      */
@@ -83,6 +82,7 @@
     /**
      * INFO.
      */
+    //note: we've aligned the int values of the java.util.logging INFO level with log4j's level
     public static final UtilLoggingLevel INFO =
             new UtilLoggingLevel(INFO_INT, "INFO", 5);
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org