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 ce...@apache.org on 2005/01/31 21:48:06 UTC

cvs commit: logging-log4j/tests/src/java/org/apache/log4j/lbel EventEvaluationTest.java

ceki        2005/01/31 12:48:06

  Modified:    src/java/org/apache/log4j/lbel/comparator
                        PropertyComparator.java MessageComparator.java
                        StringComparator.java
               src/java/org/apache/log4j/lbel Token.java Parser.java
                        TokenStream.java
               tests/src/java/org/apache/log4j/lbel
                        EventEvaluationTest.java
  Added:       src/java/org/apache/log4j/lbel/comparator
                        ClassComparator.java MethodComparator.java
  Log:
  Added support for 'CLASS' and 'METHOD' field comparisons.  This took less than 10 minutes to implement (including the test cases).
  
  Revision  Changes    Path
  1.2       +0 -6      logging-log4j/src/java/org/apache/log4j/lbel/comparator/PropertyComparator.java
  
  Index: PropertyComparator.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/lbel/comparator/PropertyComparator.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PropertyComparator.java	31 Jan 2005 20:30:44 -0000	1.1
  +++ PropertyComparator.java	31 Jan 2005 20:48:06 -0000	1.2
  @@ -14,12 +14,6 @@
    * limitations under the License.
    */
   
  -/*
  - * Created on Jan 27, 2005
  - *
  - * To change the template for this generated file go to
  - * Window>Preferences>Java>Code Generation>Code and Comments
  - */
   package org.apache.log4j.lbel.comparator;
   
   import org.apache.log4j.lbel.Operator;
  
  
  
  1.5       +0 -6      logging-log4j/src/java/org/apache/log4j/lbel/comparator/MessageComparator.java
  
  Index: MessageComparator.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/lbel/comparator/MessageComparator.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- MessageComparator.java	31 Jan 2005 20:30:44 -0000	1.4
  +++ MessageComparator.java	31 Jan 2005 20:48:06 -0000	1.5
  @@ -14,12 +14,6 @@
    * limitations under the License.
    */
   
  -/*
  - * Created on Jan 27, 2005
  - *
  - * To change the template for this generated file go to
  - * Window>Preferences>Java>Code Generation>Code and Comments
  - */
   package org.apache.log4j.lbel.comparator;
   
   import org.apache.log4j.lbel.Operator;
  
  
  
  1.2       +9 -4      logging-log4j/src/java/org/apache/log4j/lbel/comparator/StringComparator.java
  
  Index: StringComparator.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/lbel/comparator/StringComparator.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- StringComparator.java	31 Jan 2005 20:30:44 -0000	1.1
  +++ StringComparator.java	31 Jan 2005 20:48:06 -0000	1.2
  @@ -16,11 +16,10 @@
   
   
   /**
  - * Compare the logger of an event passed as parameter to the logger name and 
  - * comparison operator set in the constructor. 
  + * Base class for string-based comparators. 
    * 
  - * <p>Allowed comparison operators are '=', '!=', '>', '>=', '<', '<=', '~' and
  - * '!~' where '~' stands for regular expression match.
  + * <p>Allowed comparison operators are 'CHILDOF', '=', '!=', '>', '>=', '<', 
  + * '<=', '~' and '!~' where '~' stands for regular expression match.
    * 
    * @author <a href="http://www.qos.ch/log4j/">Ceki G&uuml;lc&uuml;</a>
    * @author Scott Deboy
  @@ -33,6 +32,12 @@
     Pattern rightSidePattern;
     Perl5Matcher matcher;
   
  +  /**
  +   * Derived classes supply the left side of the comparison based on the event.
  +   * 
  +   * @param event
  +   * @return the left side of the expression
  +   */
     abstract protected String getLeftSide(LoggingEvent event);
     
     public StringComparator(Operator operator, String rightSide) throws ScanError  {
  
  
  
  1.1                  logging-log4j/src/java/org/apache/log4j/lbel/comparator/ClassComparator.java
  
  Index: ClassComparator.java
  ===================================================================
  /*
   * Copyright 1999,2004 The Apache Software Foundation.
   *
   * Licensed 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.lbel.comparator;
  
  import org.apache.log4j.lbel.Operator;
  import org.apache.log4j.lbel.ScanError;
  import org.apache.log4j.spi.LoggingEvent;
  import org.apache.log4j.spi.location.LocationInfo;
  
  
  /**
   * Compare the class of an event passed as parameter to the class name and
   * comparison operator set in the constructor.
   *
   * <p>Allowed comparison operators are '=', '!=', '>', '>=', '<', '<=', '~' and
   * '!~' where '~' stands for regular expression match.
   *
   * @author <a href="http://www.qos.ch/log4j/">Ceki G&uuml;lc&uuml;</a>
   */
  public class ClassComparator extends StringComparator {
    
    public ClassComparator(final Operator operator, String rightSide)
      throws ScanError {
      super(operator, rightSide);
    }
  
    protected String getLeftSide(LoggingEvent event) {
      LocationInfo li = event.getLocationInformation();
      return li.getClassName();
    }
  }
  
  
  
  1.1                  logging-log4j/src/java/org/apache/log4j/lbel/comparator/MethodComparator.java
  
  Index: MethodComparator.java
  ===================================================================
  /*
   * Copyright 1999,2004 The Apache Software Foundation.
   *
   * Licensed 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.lbel.comparator;
  
  import org.apache.log4j.lbel.Operator;
  import org.apache.log4j.lbel.ScanError;
  import org.apache.log4j.spi.LoggingEvent;
  import org.apache.log4j.spi.location.LocationInfo;
  
  
  /**
   * Compare the method of an event passed as parameter to the method name and
   * comparison operator set in the constructor.
   *
   * <p>Allowed comparison operators are '=', '!=', '>', '>=', '<', '<=', '~' and
   * '!~' where '~' stands for regular expression match.
   *
   * @author <a href="http://www.qos.ch/log4j/">Ceki G&uuml;lc&uuml;</a>
   */
  public class MethodComparator extends StringComparator {
    
    public MethodComparator(final Operator operator, String rightSide)
      throws ScanError {
      super(operator, rightSide);
    }
  
    protected String getLeftSide(LoggingEvent event) {
      LocationInfo li = event.getLocationInformation();
      return li.getMethodName();
    }
  }
  
  
  
  1.4       +4 -0      logging-log4j/src/java/org/apache/log4j/lbel/Token.java
  
  Index: Token.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/lbel/Token.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Token.java	27 Jan 2005 19:31:38 -0000	1.3
  +++ Token.java	31 Jan 2005 20:48:06 -0000	1.4
  @@ -22,6 +22,10 @@
     public static final int THREAD = 140;
     public static final int PROPERTY = 150;
     public static final int DATE = 160;
  +
  +  public static final int CLASS = 170;
  +  public static final int METHOD = 180;
  +
     public static final int EOF = 1000;
   	
   	public Token(int type) {
  
  
  
  1.8       +14 -0     logging-log4j/src/java/org/apache/log4j/lbel/Parser.java
  
  Index: Parser.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/lbel/Parser.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- Parser.java	31 Jan 2005 20:30:44 -0000	1.7
  +++ Parser.java	31 Jan 2005 20:48:06 -0000	1.8
  @@ -4,9 +4,11 @@
   import java.util.Stack;
   
   import org.apache.log4j.Level;
  +import org.apache.log4j.lbel.comparator.ClassComparator;
   import org.apache.log4j.lbel.comparator.LevelComparator;
   import org.apache.log4j.lbel.comparator.LoggerComparator;
   import org.apache.log4j.lbel.comparator.MessageComparator;
  +import org.apache.log4j.lbel.comparator.MethodComparator;
   import org.apache.log4j.lbel.comparator.PropertyComparator;
   import org.apache.log4j.lbel.comparator.TimestampComparator;
   
  @@ -181,6 +183,18 @@
         ts.next();
         literal = getLiteral();
         return new Node(Node.COMPARATOR, new MessageComparator(operator, literal));
  +    case Token.METHOD:
  +      ts.next();
  +      operator = getOperator();
  +      ts.next();
  +      literal = getLiteral();
  +      return new Node(Node.COMPARATOR, new MethodComparator(operator, literal));
  +    case Token.CLASS:
  +      ts.next();
  +      operator = getOperator();
  +      ts.next();
  +      literal = getLiteral();
  +      return new Node(Node.COMPARATOR, new ClassComparator(operator, literal));
       case Token.TIMESTAMP:
         ts.next();
         operator = getOperator();
  
  
  
  1.6       +2 -0      logging-log4j/src/java/org/apache/log4j/lbel/TokenStream.java
  
  Index: TokenStream.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/lbel/TokenStream.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TokenStream.java	31 Jan 2005 20:30:44 -0000	1.5
  +++ TokenStream.java	31 Jan 2005 20:48:06 -0000	1.6
  @@ -41,6 +41,8 @@
       keywordMap.put("logger", new Token(Token.LOGGER, "logger"));
       keywordMap.put("message", new Token(Token.MESSAGE, "message"));
       keywordMap.put("level", new Token(Token.LEVEL, "level"));
  +    keywordMap.put("method", new Token(Token.METHOD, "method"));
  +    keywordMap.put("class", new Token(Token.CLASS, "class"));
       keywordMap.put("thread", new Token(Token.THREAD, "thread"));
       keywordMap.put("date", new Token(Token.DATE, "date"));
     }
  
  
  
  1.4       +28 -0     logging-log4j/tests/src/java/org/apache/log4j/lbel/EventEvaluationTest.java
  
  Index: EventEvaluationTest.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/tests/src/java/org/apache/log4j/lbel/EventEvaluationTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- EventEvaluationTest.java	31 Jan 2005 20:31:09 -0000	1.3
  +++ EventEvaluationTest.java	31 Jan 2005 20:48:06 -0000	1.4
  @@ -23,6 +23,7 @@
   
   import org.apache.log4j.Level;
   import org.apache.log4j.spi.LoggingEvent;
  +import org.apache.log4j.spi.location.LocationInfo;
   
   
   
  @@ -41,6 +42,9 @@
       event.setMessage("hello world");
       event.setLoggerName("org.wombat");
       event.setProperty("x", "y follows x");
  +      
  +    LocationInfo li = new LocationInfo("file", "org.wombat", "getWombat", "10");
  +    event.setLocationInformation(li);
     }
   
     protected void tearDown() throws Exception {
  @@ -143,6 +147,30 @@
       assertTrue(!evaluator.evaluate(event));    
     }
     
  +  public void testMethod() throws ScanError {
  +    evaluator = new LBELEventEvaluator("method = getWombat");
  +    assertTrue(evaluator.evaluate(event));
  + 
  +    evaluator = new LBELEventEvaluator("method >= get");
  +    assertTrue(evaluator.evaluate(event));
  +
  +    evaluator = new LBELEventEvaluator("method <= get");
  +    assertTrue(!evaluator.evaluate(event));
  +    
  +  }
  +
  +  public void testClass() throws ScanError {
  +    evaluator = new LBELEventEvaluator("class = org.wombat");
  +    assertTrue(evaluator.evaluate(event));
  +    
  +    evaluator = new LBELEventEvaluator("class > org");
  +    assertTrue(evaluator.evaluate(event));
  +    
  +    evaluator = new LBELEventEvaluator("class < org");
  +    assertTrue(!evaluator.evaluate(event));
  +
  +  }
  +  
     public static Test XXsuite() {
       TestSuite suite = new TestSuite();
       //suite.addTest(new ParserTest("testAndOr"));
  
  
  

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