View Javadoc

1   package ch.qos.logback.classic.filter;
2   
3   import ch.qos.logback.classic.Level;
4   import ch.qos.logback.classic.spi.LoggingEvent;
5   import ch.qos.logback.core.filter.Filter;
6   import ch.qos.logback.core.spi.FilterReply;
7   
8   /**
9    * A class that filters events depending on their level.
10   * 
11   * All events with a level under or above the specified
12   * level will be denied, while all events with a level
13   * equal or above the specified level will trigger a
14   * FilterReply.NEUTRAL result, to allow the rest of the
15   * filter chain process the event.
16   * 
17   * For more information about filters, please refer to the online manual at
18   * http://logback.qos.ch/manual/filters.html
19   *
20   * @author Sébastien Pennec
21   */
22  public class ThresholdFilter extends Filter {
23  
24    Level level;
25    
26    @Override
27    public FilterReply decide(Object eventObject) {
28      if (!isStarted()) {
29        return FilterReply.NEUTRAL;
30      }
31      
32      LoggingEvent event = (LoggingEvent)eventObject;
33      
34      if (event.getLevel().isGreaterOrEqual(level)) {
35        return FilterReply.NEUTRAL;
36      } else {
37        return FilterReply.DENY;
38      }
39    }
40    
41    public void setLevel(String level) {
42      this.level = Level.toLevel(level);
43    }
44    
45    public void start() {
46      if (this.level != null) {
47        super.start();
48      }
49    }
50  }