You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-cvs@jakarta.apache.org by mw...@apache.org on 2002/05/20 08:20:32 UTC

cvs commit: jakarta-log4j/src/java/org/apache/log4j/varia NDCMatchFilter.java

mwomack     02/05/19 23:20:32

  Added:       src/java/org/apache/log4j/varia NDCMatchFilter.java
  Log:
  Added NDCMatchFilter which can be used to select LoggingEvents based on exact or contained matches of its NDC value.
  
  Revision  Changes    Path
  1.1                  jakarta-log4j/src/java/org/apache/log4j/varia/NDCMatchFilter.java
  
  Index: NDCMatchFilter.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.APL file.
   */
  
  package org.apache.log4j.varia;
  
  import org.apache.log4j.NDC;
  import org.apache.log4j.spi.Filter;
  import org.apache.log4j.spi.LoggingEvent;
  import org.apache.log4j.helpers.OptionConverter;
  
  /**
     This is a simple filter based on {@link NDC} value matching.
  
     <p>The filter admits three options <b>ValueToMatch</b> and
     <b>ExactMatch</b> and <b>AcceptOnMatch</b>. If <b>ExactMatch</b>
     is set to true, then the <b>ValueToMatch</b> must exactly match
     the NDC value of the {@link LoggingEvent}. By default the filter
     will match if the <b>ValueToMatch</b> is "contained" in the NDC
     value. Also by default, the <b>ValueToMatch</b> is set to null
     and will only match when the NDC stack is empty.
     
     <p>If the filter matches between the configured 
     <b>ValueToMatch</b> and the NDC value of the {@link 
     LoggingEvent}, then the {@link #decide} method returns {@link 
     Filter#ACCEPT} in case the <b>AcceptOnMatch</b> option value 
     is set to <code>true</code>, if it is <code>false</code> then 
     {@link Filter#DENY} is returned. If there is no match, 
     {@link Filter#NEUTRAL} is returned.
  
     @author Mark Womack
  
     @since 1.3 */
  public class NDCMatchFilter extends Filter {
    
    /**
       Do we return ACCEPT when a match occurs. Default is
       <code>true</code>.  */
    boolean acceptOnMatch = true;
  
    /**
      The value to match in the NDC value of the LoggingEvent. */
    String valueToMatch;
    
    /**
      Do we look for an exact match or just a "contains" match? */
    boolean exactMatch = false;
  
    /**
      Sets the value to match in the NDC value of the LoggingEvent. */
    public
    void setValueToMatch(String value) {
      valueToMatch = value;
    }
    
    /**
      Gets the value to match in the NDC value of the LoggingEvent. */
    public
    String getValueToMatch() {
      return valueToMatch;
    }
  
    /**
      Set to true if configured value must exactly match the NDC
      value of the LoggingEvent. Set to false if the configured
      value must only be contained in the NDC value of the
      LoggingEvent. Default is false. */
    public
    void setExactMatch(boolean exact) {
      exactMatch = exact;
    }
    
    public
    boolean getExactMatch() {
      return exactMatch;
    }
  
    /**
      If true, then the filter will return {@link Filter#ACCEPT}
      if the filter matches the configured value. If false, then
      {@link Filter#DENY} is returned. Default setting is true. */
    public
    void setAcceptOnMatch(boolean acceptOnMatch) {
      this.acceptOnMatch = acceptOnMatch;
    }
    
    /**
      Gets the AcceptOnMatch value. */
    public
    boolean getAcceptOnMatch() {
      return acceptOnMatch;
    }
    
    /**
       Return the decision of this filter.
  
       If there is a match, then the returned decision is {@link 
       Filter#ACCEPT} if the <b>AcceptOnMatch</b> property is set 
       to <code>true</code>. The returned decision is {@link 
       Filter#DENY} if the <b>AcceptOnMatch</b> property is set 
       to false. */
    public
    int decide(LoggingEvent event) {
      
      // get the ndc value for the event
      String eventNDC = event.getNDC();
      
      // check for a match
      boolean matchOccured = false;
      if (eventNDC == null) {
        if (valueToMatch == null)
          matchOccured = true;
      } else {
        if (valueToMatch != null) {
          if (exactMatch) {
            matchOccured = eventNDC.equals(valueToMatch);
          } else {
            matchOccured = (eventNDC.indexOf(valueToMatch) != -1);
          }
        }
      }
  
      if(matchOccured) {  
        if(this.acceptOnMatch)
          return Filter.ACCEPT;
        else
          return Filter.DENY;
      } else {
        return Filter.NEUTRAL;
      }
    }
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


[PATCH] jakarta-log4j/src/java/org/apache/log4j/varia NDCMatchFilter.java

Posted by Ceki Gülcü <ce...@qos.ch>.
Mark,

Good work on the NDCMatchFilter and the associated test cases. Here is
an attempt to improve on the NDCMatchFilter documentation. Other
filters should also benefit from similar changes. You might want to
apply this patch in case you find that it improves on the existing.

Index: src/java/org/apache/log4j/varia/NDCMatchFilter.java
===================================================================
RCS file: 
/home/cvs/jakarta-log4j/src/java/org/apache/log4j/varia/NDCMatchFilter.java,v
retrieving revision 1.2
diff -u -r1.2 NDCMatchFilter.java
--- src/java/org/apache/log4j/varia/NDCMatchFilter.java 20 May 2002 
09:52:58 -0000      1.2
+++ src/java/org/apache/log4j/varia/NDCMatchFilter.java 20 May 2002 
12:22:00 -0000
@@ -16,20 +16,26 @@
     This is a simple filter based on {@link NDC} value matching.

     <p>The filter admits three options <b>ValueToMatch</b> and
-   <b>ExactMatch</b> and <b>AcceptOnMatch</b>. If <b>ExactMatch</b>
-   is set to true, then the <b>ValueToMatch</b> must exactly match
-   the NDC value of the {@link LoggingEvent}. By default the filter
-   will match if the <b>ValueToMatch</b> is "contained" in the NDC
-   value. Also by default, the <b>ValueToMatch</b> is set to null
-   and will only match when the NDC stack is empty.
+   <b>ExactMatch</b> and <b>AcceptOnMatch</b>.

-   <p>If the filter matches between the configured
-   <b>ValueToMatch</b> and the NDC value of the {@link
-   LoggingEvent}, then the {@link #decide} method returns {@link
-   Filter#ACCEPT} in case the <b>AcceptOnMatch</b> option value
-   is set to <code>true</code>, if it is <code>false</code> then
-   {@link Filter#DENY} is returned. If there is no match,
-   {@link Filter#NEUTRAL} is returned.
+   <p>If a match occurs, then the {@link #decide decide} method
+   returns {@link Filter#ACCEPT} but only in case the
+   <b>AcceptOnMatch</b> option value is set to <code>true</code>, if
+   it is set to <code>false</code> then {@link Filter#DENY} is
+   returned. If no match occurs, the {@link #decide decide} method
+   returns {@link Filter#NEUTRAL}.
+
+   <p>As the name indicates, the value of <b>ValueToMatch</b> property
+   determines the string value to match. If <b>ExactMatch</b> is set
+   to true, a match will occur only when the value to match exactly
+   matches the NDC value of the logging event.  Otherwise, if the
+   <b>ExactMatch</b> property is set to <code>false</code>, a match
+   will occur if the NDC value is contained anywhere within the value
+   to match. The <b>ExactMatch</b> property is set to
+   <code>false</code> by default.
+
+   <p>Note that by default the value to match is set to
+   <code>null</code> and will only match an empty NDC stack.

     @author Mark Womack




At 06:20 20.05.2002 +0000, mwomack@ apache.org wrote:
>mwomack     02/05/19 23:20:32
>
>   Added:       src/java/org/apache/log4j/varia NDCMatchFilter.java
>   Log:
>   Added NDCMatchFilter which can be used to select LoggingEvents based on 
> exact or contained matches of its NDC value.
>
>   Revision  Changes    Path
>   1.1 
> jakarta-log4j/src/java/org/apache/log4j/varia/NDCMatchFilter.java
>
>   Index: NDCMatchFilter.java
>   ===================================================================
>   /*
>    * Copyright (C) The Apache Software Foundation. All rights reserved.
>    *
>    * This software is published under the terms of the Apache Software 
> License
>    * version 1.1, a copy of which has been included  with this 
> distribution in
>    * the LICENSE.APL file.
>    */
>
>   package org.apache.log4j.varia;
>
>   import org.apache.log4j.NDC;
>   import org.apache.log4j.spi.Filter;
>   import org.apache.log4j.spi.LoggingEvent;
>   import org.apache.log4j.helpers.OptionConverter;
>
>   /**
>      This is a simple filter based on {@link NDC} value matching.
>
>      <p>The filter admits three options <b>ValueToMatch</b> and
>      <b>ExactMatch</b> and <b>AcceptOnMatch</b>. If <b>ExactMatch</b>
>      is set to true, then the <b>ValueToMatch</b> must exactly match
>      the NDC value of the {@link LoggingEvent}. By default the filter
>      will match if the <b>ValueToMatch</b> is "contained" in the NDC
>      value. Also by default, the <b>ValueToMatch</b> is set to null
>      and will only match when the NDC stack is empty.
>
>      <p>If the filter matches between the configured
>      <b>ValueToMatch</b> and the NDC value of the {@link
>      LoggingEvent}, then the {@link #decide} method returns {@link
>      Filter#ACCEPT} in case the <b>AcceptOnMatch</b> option value
>      is set to <code>true</code>, if it is <code>false</code> then
>      {@link Filter#DENY} is returned. If there is no match,
>      {@link Filter#NEUTRAL} is returned.
>
>      @author Mark Womack
>
>      @since 1.3 */
>   public class NDCMatchFilter extends Filter {
>
>     /**
>        Do we return ACCEPT when a match occurs. Default is
>        <code>true</code>.  */
>     boolean acceptOnMatch = true;
>
>     /**
>       The value to match in the NDC value of the LoggingEvent. */
>     String valueToMatch;
>
>     /**
>       Do we look for an exact match or just a "contains" match? */
>     boolean exactMatch = false;
>
>     /**
>       Sets the value to match in the NDC value of the LoggingEvent. */
>     public
>     void setValueToMatch(String value) {
>       valueToMatch = value;
>     }
>
>     /**
>       Gets the value to match in the NDC value of the LoggingEvent. */
>     public
>     String getValueToMatch() {
>       return valueToMatch;
>     }
>
>     /**
>       Set to true if configured value must exactly match the NDC
>       value of the LoggingEvent. Set to false if the configured
>       value must only be contained in the NDC value of the
>       LoggingEvent. Default is false. */
>     public
>     void setExactMatch(boolean exact) {
>       exactMatch = exact;
>     }
>
>     public
>     boolean getExactMatch() {
>       return exactMatch;
>     }
>
>     /**
>       If true, then the filter will return {@link Filter#ACCEPT}
>       if the filter matches the configured value. If false, then
>       {@link Filter#DENY} is returned. Default setting is true. */
>     public
>     void setAcceptOnMatch(boolean acceptOnMatch) {
>       this.acceptOnMatch = acceptOnMatch;
>     }
>
>     /**
>       Gets the AcceptOnMatch value. */
>     public
>     boolean getAcceptOnMatch() {
>       return acceptOnMatch;
>     }
>
>     /**
>        Return the decision of this filter.
>
>        If there is a match, then the returned decision is {@link
>        Filter#ACCEPT} if the <b>AcceptOnMatch</b> property is set
>        to <code>true</code>. The returned decision is {@link
>        Filter#DENY} if the <b>AcceptOnMatch</b> property is set
>        to false. */
>     public
>     int decide(LoggingEvent event) {
>
>       // get the ndc value for the event
>       String eventNDC = event.getNDC();
>
>       // check for a match
>       boolean matchOccured = false;
>       if (eventNDC == null) {
>         if (valueToMatch == null)
>           matchOccured = true;
>       } else {
>         if (valueToMatch != null) {
>           if (exactMatch) {
>             matchOccured = eventNDC.equals(valueToMatch);
>           } else {
>             matchOccured = (eventNDC.indexOf(valueToMatch) != -1);
>           }
>         }
>       }
>
>       if(matchOccured) {
>         if(this.acceptOnMatch)
>           return Filter.ACCEPT;
>         else
>           return Filter.DENY;
>       } else {
>         return Filter.NEUTRAL;
>       }
>     }
>   }
>

--
Ceki


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>