View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.filter;
15  
16  import ch.qos.logback.classic.Level;
17  import ch.qos.logback.classic.spi.ILoggingEvent;
18  import ch.qos.logback.core.filter.Filter;
19  import ch.qos.logback.core.spi.FilterReply;
20  
21  /**
22   * Filters events below the threshold level.
23   * 
24   * Events with a level below the specified level will be denied, while events
25   * with a level equal or above the specified level will trigger a
26   * FilterReply.NEUTRAL result, to allow the rest of the filter chain process the
27   * event.
28   * 
29   * For more information about filters, please refer to the online manual at
30   * http://logback.qos.ch/manual/filters.html#thresholdFilter
31   *
32   * @author Sébastien Pennec
33   */
34  public class ThresholdFilter extends Filter<ILoggingEvent> {
35  
36      Level level;
37  
38      @Override
39      public FilterReply decide(ILoggingEvent event) {
40          if (!isStarted()) {
41              return FilterReply.NEUTRAL;
42          }
43  
44          if (event.getLevel().isGreaterOrEqual(level)) {
45              return FilterReply.NEUTRAL;
46          } else {
47              return FilterReply.DENY;
48          }
49      }
50  
51      public void setLevel(String level) {
52          this.level = Level.toLevel(level);
53      }
54  
55      public void start() {
56          if (this.level != null) {
57              super.start();
58          }
59      }
60  }