View Javadoc

1   package ch.qos.logback.core.filter;
2   
3   import ch.qos.logback.core.boolex.EvaluationException;
4   import ch.qos.logback.core.boolex.EventEvaluator;
5   import ch.qos.logback.core.spi.FilterReply;
6   
7   /**
8    * The value of the {@link #onMatch} and {@link #onMismatch} attributes is set
9    * to {@link Filter.NEUTRAL}, so that a badly configured evaluator filter does
10   * not disturb the functioning of the filter chain. 
11   * 
12   * <p>It is expected that one of the two attributes will have its value changed
13   * to {@link Filter.ACCEPT} or {@link Filter.DENY}. That way, it is possible to
14   * decide if a given result must be returned after the evaluation either failed
15   * or succeeded.
16   * 
17   * 
18   * <p> For more information about filters, please refer to the online manual at
19   * http://logback.qos.ch/manual/filters.html
20   * 
21   * @author Ceki G&uuml;lc&uuml;
22   * @author S&eacute;bastien Pennec
23   */
24  public class EvaluatorFilter<E> extends AbstractMatcherFilter<E> {
25  
26    EventEvaluator<E> evaluator;
27  
28    @Override
29    public void start() {
30      if (evaluator != null) {
31        super.start();
32      } else {
33        addError("No evaluator set for filter " + this.getName());
34      }
35    }
36  
37    public EventEvaluator<E> getEvaluator() {
38      return evaluator;
39    }
40  
41    public void setEvaluator(EventEvaluator<E> evaluator) {
42      this.evaluator = evaluator;
43    }
44  
45    public FilterReply decide(E event) {
46      // let us not throw an exception
47      // see also bug #17.
48      if (!isStarted() || !evaluator.isStarted()) {
49        return FilterReply.NEUTRAL;
50      }
51      try {
52        if (evaluator.evaluate(event)) {
53          return onMatch;
54        } else {
55          return onMismatch;
56        }
57      } catch (EvaluationException e) {
58        addError("Evaluator " + evaluator.getName() + " threw an exception", e);
59        return FilterReply.NEUTRAL;
60      }
61    }
62  
63  }