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