KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > varia > LevelMatchFilter


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.varia;
18
19 import org.apache.log4j.Level;
20 import org.apache.log4j.spi.Filter;
21 import org.apache.log4j.spi.LoggingEvent;
22 import org.apache.log4j.helpers.OptionConverter;
23
24 /**
25    This is a very simple filter based on level matching.
26
27    <p>The filter admits two options <b>LevelToMatch</b> and
28    <b>AcceptOnMatch</b>. If there is an exact match between the value
29    of the <b>LevelToMatch</b> option and the level of the {@link
30    LoggingEvent}, then the {@link #decide} method returns {@link
31    Filter#ACCEPT} in case the <b>AcceptOnMatch</b> option value is set
32    to <code>true</code>, if it is <code>false</code> then {@link
33    Filter#DENY} is returned. If there is no match, {@link
34    Filter#NEUTRAL} is returned.
35
36    @author Ceki G&uuml;lc&uuml;
37
38    @since 1.2 */

39 public class LevelMatchFilter extends Filter {
40   
41   /**
42      Do we return ACCEPT when a match occurs. Default is
43      <code>true</code>. */

44   boolean acceptOnMatch = true;
45
46   /**
47    */

48   Level levelToMatch;
49
50  
51   public
52   void setLevelToMatch(String JavaDoc level) {
53     levelToMatch = OptionConverter.toLevel(level, null);
54   }
55   
56   public
57   String JavaDoc getLevelToMatch() {
58     return levelToMatch == null ? null : levelToMatch.toString();
59   }
60   
61   public
62   void setAcceptOnMatch(boolean acceptOnMatch) {
63     this.acceptOnMatch = acceptOnMatch;
64   }
65   
66   public
67   boolean getAcceptOnMatch() {
68     return acceptOnMatch;
69   }
70   
71
72   /**
73      Return the decision of this filter.
74
75      Returns {@link Filter#NEUTRAL} if the <b>LevelToMatch</b> option
76      is not set or if there is not match. Otherwise, if there is a
77      match, then the returned decision is {@link Filter#ACCEPT} if the
78      <b>AcceptOnMatch</b> property is set to <code>true</code>. The
79      returned decision is {@link Filter#DENY} if the
80      <b>AcceptOnMatch</b> property is set to false.
81
82   */

83   public
84   int decide(LoggingEvent event) {
85     if(this.levelToMatch == null) {
86       return Filter.NEUTRAL;
87     }
88     
89     boolean matchOccured = false;
90     if(this.levelToMatch.equals(event.getLevel())) {
91       matchOccured = true;
92     }
93
94     if(matchOccured) {
95       if(this.acceptOnMatch)
96       return Filter.ACCEPT;
97       else
98       return Filter.DENY;
99     } else {
100       return Filter.NEUTRAL;
101     }
102   }
103 }
104
Popular Tags