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 2003/10/24 10:04:03 UTC

cvs commit: jakarta-log4j/src/java/org/apache/log4j/chainsaw/rule LevelInequalityRule.java InequalityRule.java ExpressionRule.java RuleFactory.java InFixToPostFix.java

sdeboy      2003/10/24 01:04:03

  Modified:    src/java/org/apache/log4j/chainsaw/rule ExpressionRule.java
                        RuleFactory.java InFixToPostFix.java
  Added:       src/java/org/apache/log4j/chainsaw/rule
                        LevelInequalityRule.java InequalityRule.java
  Log:
  Added inequality rules - one that attempts to convert the event field's values to longs and perform inequality evaluations, and one that evaluates levels (both log4j and util.logging) using toInt
  
  Revision  Changes    Path
  1.3       +29 -29    jakarta-log4j/src/java/org/apache/log4j/chainsaw/rule/ExpressionRule.java
  
  Index: ExpressionRule.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/chainsaw/rule/ExpressionRule.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ExpressionRule.java	23 Oct 2003 09:32:23 -0000	1.2
  +++ ExpressionRule.java	24 Oct 2003 08:04:03 -0000	1.3
  @@ -90,35 +90,35 @@
   }
   
   
  -/**
  - * Evaluate a boolean postfix expression.
  - *
  - */
  -class PostFixExpressionCompiler {
  -
  -  Rule compileExpression(String expression) {
  -    System.out.println("compiling expression: " + expression);
  -
  -    Stack stack = new Stack();
  -    Enumeration tokenizer = new StringTokenizer(expression);
  -
  -    while (tokenizer.hasMoreElements()) {
  -      //examine each token
  -      String nextToken = ((String) tokenizer.nextElement()).toLowerCase();
  -
  -      //if a symbol is found, pop 2 off the stack, evaluate and push the result 
  -      if (RuleFactory.isRule(nextToken)) {
  -        Rule r = (Rule) RuleFactory.getRule(nextToken, stack);
  -        System.out.println("pushing rule " + r);
  -        stack.push(r);
  -      } else {
  -        System.out.println("pushing token " + nextToken);
  -
  -        //variables or constants are pushed onto the stack
  -        stack.push(nextToken);
  +  /**
  +   * Evaluate a boolean postfix expression.
  +   *
  +   */
  +  class PostFixExpressionCompiler {
  +
  +    Rule compileExpression(String expression) {
  +      System.out.println("compiling expression: " + expression);
  +
  +      Stack stack = new Stack();
  +      Enumeration tokenizer = new StringTokenizer(expression);
  +
  +      while (tokenizer.hasMoreElements()) {
  +        //examine each token
  +        String nextToken = ((String) tokenizer.nextElement());
  +
  +        //if a symbol is found, pop 2 off the stack, evaluate and push the result 
  +        if (RuleFactory.isRule(nextToken)) {
  +          Rule r = (Rule) RuleFactory.getRule(nextToken, stack);
  +          System.out.println("pushing rule " + r);
  +          stack.push(r);
  +        } else {
  +          System.out.println("pushing token " + nextToken);
  +
  +          //variables or constants are pushed onto the stack
  +          stack.push(nextToken);
  +        }
         }
  -    }
   
  -    return (Rule)stack.pop();
  -  }
  +      return (Rule)stack.pop();
  +    }
   }
  
  
  
  1.3       +23 -1     jakarta-log4j/src/java/org/apache/log4j/chainsaw/rule/RuleFactory.java
  
  Index: RuleFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/chainsaw/rule/RuleFactory.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- RuleFactory.java	24 Oct 2003 04:05:23 -0000	1.2
  +++ RuleFactory.java	24 Oct 2003 08:04:03 -0000	1.3
  @@ -68,7 +68,10 @@
     private static final String EQUALS_RULE = "==";
     private static final String PARTIAL_TEXT_MATCH_RULE = "~=";
     private static final String LIKE_RULE = "like";
  -
  +  private static final String LESS_THAN_RULE = "<";
  +  private static final String GREATER_THAN_RULE = ">";
  +  private static final String LESS_THAN_EQUALS_RULE = "<=";
  +  private static final String GREATER_THAN_EQUALS_RULE = ">=";
     static {
       rules.add(AND_RULE);
       rules.add(OR_RULE);
  @@ -77,6 +80,10 @@
       rules.add(EQUALS_RULE);
       rules.add(PARTIAL_TEXT_MATCH_RULE);
       rules.add(LIKE_RULE);
  +    rules.add(LESS_THAN_RULE);
  +    rules.add(GREATER_THAN_RULE);
  +    rules.add(LESS_THAN_EQUALS_RULE);
  +    rules.add(GREATER_THAN_EQUALS_RULE);
     }
   
     static boolean isRule(String symbol) {
  @@ -112,6 +119,21 @@
         return LikeRule.getRule(stack);
       }
   
  +    if (LESS_THAN_RULE.equals(symbol)) {
  +      return InequalityRule.getRule(LESS_THAN_RULE, stack);
  +    }
  +
  +    if (GREATER_THAN_RULE.equals(symbol)) {
  +      return InequalityRule.getRule(GREATER_THAN_RULE, stack);
  +    }
  +
  +    if (LESS_THAN_EQUALS_RULE.equals(symbol)) {
  +      return InequalityRule.getRule(LESS_THAN_EQUALS_RULE, stack);
  +    }
  +
  +    if (GREATER_THAN_EQUALS_RULE.equals(symbol)) {
  +      return InequalityRule.getRule(GREATER_THAN_EQUALS_RULE, stack);
  +    }
       return null;
     }
   }
  
  
  
  1.2       +11 -2     jakarta-log4j/src/java/org/apache/log4j/chainsaw/rule/InFixToPostFix.java
  
  Index: InFixToPostFix.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/chainsaw/rule/InFixToPostFix.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- InFixToPostFix.java	23 Oct 2003 09:18:12 -0000	1.1
  +++ InFixToPostFix.java	24 Oct 2003 08:04:03 -0000	1.2
  @@ -79,12 +79,21 @@
       operators.add("||");
       operators.add("&&");
       operators.add("like");
  -
  +    operators.add("<");
  +    operators.add(">");
  +    operators.add("<=");
  +    operators.add(">=");
  +    
       operators.add("*");
       operators.add("+");
       operators.add("-");
   
       //boolean precedence
  +    precedenceMap.put("<", new Integer(3));
  +    precedenceMap.put(">", new Integer(3));
  +    precedenceMap.put("<=", new Integer(3));
  +    precedenceMap.put(">=", new Integer(3));
  +    
       precedenceMap.put("!", new Integer(3));
       precedenceMap.put("!=", new Integer(3));
       precedenceMap.put("==", new Integer(3));
  @@ -139,7 +148,7 @@
       Stack stack = new Stack();
   
       while (tokenizer.hasMoreTokens()) {
  -      String token = tokenizer.nextToken().toLowerCase();
  +      String token = tokenizer.nextToken();
         System.out.println("FOUND TOKEN " + token);
   
         if ("(".equals(token)) {
  
  
  
  1.1                  jakarta-log4j/src/java/org/apache/log4j/chainsaw/rule/LevelInequalityRule.java
  
  Index: LevelInequalityRule.java
  ===================================================================
  /*
   * ============================================================================
   *                   The Apache Software License, Version 1.1
   * ============================================================================
   *
   *    Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without modifica-
   * tion, are permitted provided that the following conditions are met:
   *
   * 1. Redistributions of  source code must  retain the above copyright  notice,
   *    this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright notice,
   *    this list of conditions and the following disclaimer in the documentation
   *    and/or other materials provided with the distribution.
   *
   * 3. The end-user documentation included with the redistribution, if any, must
   *    include  the following  acknowledgment:  "This product includes  software
   *    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
   *    Alternately, this  acknowledgment may  appear in the software itself,  if
   *    and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "log4j" and  "Apache Software Foundation"  must not be used to
   *    endorse  or promote  products derived  from this  software without  prior
   *    written permission. For written permission, please contact
   *    apache@apache.org.
   *
   * 5. Products  derived from this software may not  be called "Apache", nor may
   *    "Apache" appear  in their name,  without prior written permission  of the
   *    Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   * APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   * DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   * OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   * ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   * (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * This software  consists of voluntary contributions made  by many individuals
   * on  behalf of the Apache Software  Foundation.  For more  information on the
   * Apache Software Foundation, please see <http://www.apache.org/>.
   *
   */
  
  package org.apache.log4j.chainsaw.rule;
  
  import java.util.Iterator;
  import java.util.LinkedList;
  import java.util.List;
  import java.util.Stack;
  
  import org.apache.log4j.Level;
  import org.apache.log4j.UtilLoggingLevel;
  import org.apache.log4j.chainsaw.LoggingEventFieldResolver;
  import org.apache.log4j.spi.LoggingEvent;
  /**
   * A Rule class implementing inequality evaluation for Levels (log4j and util.logging) using the toInt method.
   * 
   * @author Scott Deboy <sd...@apache.org>
   */
  
  class LevelInequalityRule extends AbstractRule {
      LoggingEventFieldResolver resolver = LoggingEventFieldResolver.getInstance();
  
  	Level levelFirstParam;
        String levelSecondParam;
        List utilList = new LinkedList();
        List levelList = new LinkedList();
        String inequalitySymbol;
  
        private LevelInequalityRule(String inequalitySymbol, String levelFirstParam, String levelSecondParam) {
            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());
  
            Iterator iter = UtilLoggingLevel.getAllPossibleLevels().iterator();
            while (iter.hasNext()) {
                utilList.add(((UtilLoggingLevel)iter.next()).toString());
            }
  
          if (levelList.contains(levelFirstParam)) {
              this.levelFirstParam = Level.toLevel(levelFirstParam);
          } else {
              this.levelFirstParam = UtilLoggingLevel.toLevel(levelFirstParam);
          }
          this.inequalitySymbol = inequalitySymbol;
          this.levelSecondParam = levelSecondParam;
        }
  
        static Rule getRule(String inequalitySymbol, Stack stack) {
            String p1 = stack.pop().toString();
            String p2 = stack.pop().toString();
            return new LevelInequalityRule(inequalitySymbol, p1, p2);
        }
        
        public boolean evaluate(LoggingEvent event) {
          //use the type of the first level to access the static toLevel method on the second param
          Level level2 = levelFirstParam.toLevel(resolver.getValue(levelSecondParam, event).toString());
          System.out.println("lessthan level op " + levelFirstParam + ".." + level2);
  
          boolean result = false;
          int first = levelFirstParam.toInt();
          int second = level2.toInt();
          
          if ("<".equals(inequalitySymbol)) {
              result = first < second;
          } else if (">".equals(inequalitySymbol)) {
              result = first > second;
          } else if ("<=".equals(inequalitySymbol)){
              result = first <= second;
          } else if (">=".equals(inequalitySymbol)) {
              result = first >= second;
          }
          System.out.println("result is " + result);
  
          return result;
        }
        
    }
  
  
  1.1                  jakarta-log4j/src/java/org/apache/log4j/chainsaw/rule/InequalityRule.java
  
  Index: InequalityRule.java
  ===================================================================
  /*
   * ============================================================================
   *                   The Apache Software License, Version 1.1
   * ============================================================================
   *
   *    Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without modifica-
   * tion, are permitted provided that the following conditions are met:
   *
   * 1. Redistributions of  source code must  retain the above copyright  notice,
   *    this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright notice,
   *    this list of conditions and the following disclaimer in the documentation
   *    and/or other materials provided with the distribution.
   *
   * 3. The end-user documentation included with the redistribution, if any, must
   *    include  the following  acknowledgment:  "This product includes  software
   *    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
   *    Alternately, this  acknowledgment may  appear in the software itself,  if
   *    and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "log4j" and  "Apache Software Foundation"  must not be used to
   *    endorse  or promote  products derived  from this  software without  prior
   *    written permission. For written permission, please contact
   *    apache@apache.org.
   *
   * 5. Products  derived from this software may not  be called "Apache", nor may
   *    "Apache" appear  in their name,  without prior written permission  of the
   *    Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   * APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   * DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   * OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   * ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   * (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * This software  consists of voluntary contributions made  by many individuals
   * on  behalf of the Apache Software  Foundation.  For more  information on the
   * Apache Software Foundation, please see <http://www.apache.org/>.
   *
   */
  
  package org.apache.log4j.chainsaw.rule;
  
  import java.util.Stack;
  
  import org.apache.log4j.chainsaw.LoggingEventFieldResolver;
  import org.apache.log4j.spi.LoggingEvent;
  
  /**
   * A Rule class implementing less than - expects to be able to convert two values to longs.
   * If the field being evaluated can support inequality evaluation, the appropriate rule is returned.
   * (For example, if the expression is Level < DEBUG, a LessThanLevelRule is returned).
   * 
   * @author Scott Deboy <sd...@apache.org>
   */
  
  class InequalityRule extends AbstractRule {
      private static final String LEVEL = "LEVEL";
  
    LoggingEventFieldResolver resolver = LoggingEventFieldResolver.getInstance();
    String firstParam;
    String secondParam;
    String inequalitySymbol;
  
    private InequalityRule(String inequalitySymbol, String firstParam, String secondParam) {
      this.inequalitySymbol = inequalitySymbol;
      this.firstParam = firstParam;
      this.secondParam = secondParam;
    }
  
    static Rule getRule(String inequalitySymbol, Stack stack) {
      String p1 = stack.pop().toString();
      String p2 = stack.pop().toString();
      if (p2.equalsIgnoreCase(LEVEL)) {
          //push the value back on the stack and allow the level-specific rule pop values
          stack.push(p1);
          stack.push(p2);
  
          return LevelInequalityRule.getRule(inequalitySymbol, stack);
      } else {
          System.out.println("get equals op " + p1 + ".." + p2);
  
          return new InequalityRule(inequalitySymbol, p1, p2);
      }
    }
    
    public boolean evaluate(LoggingEvent event) {
      long second = new Long(resolver.getValue(secondParam, event).toString()).longValue();
      long first = new Long(firstParam).longValue();
  
      boolean result = false;
          
      if ("<".equals(inequalitySymbol)) {
          result = first < second;
      } else if (">".equals(inequalitySymbol)) {
          result = first > second;
      } else if ("<=".equals(inequalitySymbol)){
          result = first <= second;
      } else if (">=".equals(inequalitySymbol)) {
          result = first >= second;
      }
      System.out.println("result is " + result);
  
      return result;
    }
  }
  
  
  

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