本文主要研究一下logback的EvaluatorFilter

EvaluatorFilter

ch/qos/logback/core/filter/EvaluatorFilter.java

public class EvaluatorFilter<E> extends AbstractMatcherFilter<E> {

    EventEvaluator<E> evaluator;

    @Override
    public void start() {
        if (evaluator != null) {
            super.start();
        } else {
            addError("No evaluator set for filter " + this.getName());
        }
    }

    public EventEvaluator<E> getEvaluator() {
        return evaluator;
    }

    public void setEvaluator(EventEvaluator<E> evaluator) {
        this.evaluator = evaluator;
    }

    public FilterReply decide(E event) {
        // let us not throw an exception
        // see also bug #17.
        if (!isStarted() || !evaluator.isStarted()) {
            return FilterReply.NEUTRAL;
        }
        try {
            if (evaluator.evaluate(event)) {
                return onMatch;
            } else {
                return onMismatch;
            }
        } catch (EvaluationException e) {
            addError("Evaluator " + evaluator.getName() + " threw an exception", e);
            return FilterReply.NEUTRAL;
        }
    }

}
EvaluatorFilter继承了AbstractMatcherFilter,其decide方法在evaluator.evaluate(event)为true时返回onMatch,否则返回onMismatch,异常的话返回NEUTRAL

EventEvaluator

ch/qos/logback/core/boolex/EventEvaluator.java

public interface EventEvaluator<E> extends ContextAware, LifeCycle {

    /**
     * Evaluates whether the event passed as parameter matches some user-specified
     * criteria.
     * 
     * <p>
     * The <code>Evaluator</code> is free to evaluate the event as it pleases. In
     * particular, the evaluation results <em>may</em> depend on previous events.
     * 
     * @param event The event to evaluate
     * @return true if there is a match, false otherwise.
     * @throws NullPointerException can be thrown in presence of null values
     * @throws EvaluationException  may be thrown during faulty evaluation
     */
    boolean evaluate(E event) throws NullPointerException, EvaluationException;

    /**
     * Evaluators are named entities.
     * 
     * @return The name of this evaluator.
     */
    String getName();

    /**
     * Evaluators are named entities.
     */
    void setName(String name);
}
EventEvaluator接口定义了evaluate、getName、setName方法

EventEvaluatorBase

ch/qos/logback/core/boolex/EventEvaluatorBase.java

abstract public class EventEvaluatorBase<E> extends ContextAwareBase implements EventEvaluator<E> {

    String name;
    boolean started;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if (this.name != null) {
            throw new IllegalStateException("name has been already set");
        }
        this.name = name;
    }

    public boolean isStarted() {
        return started;
    }

    public void start() {
        started = true;
    }

    public void stop() {
        started = false;
    }

}
EventEvaluatorBase声明实现EventEvaluator,它主要是定义了name、started属性,另外还有一个抽象子类为EventEvaluatorBase,而JaninoEventEvaluatorBase抽象子类继承了EventEvaluatorBase,它们有基于IAccessEvent,也有基于ILoggingEvent。

OnMarkerEvaluator

ch/qos/logback/classic/boolex/OnMarkerEvaluator.java

public class OnMarkerEvaluator extends EventEvaluatorBase<ILoggingEvent> {

    List<String> markerList = new ArrayList<String>();

    public void addMarker(String markerStr) {
        markerList.add(markerStr);
    }

    /**
     * Return true if event passed as parameter contains one of the specified
     * user-markers.
     */
    public boolean evaluate(ILoggingEvent event) throws NullPointerException, EvaluationException {

        List<Marker> markerListInEvent = event.getMarkerList();
        if (markerListInEvent == null || markerListInEvent.isEmpty()) {
            return false;
        }

        for (String markerStr : markerList) {
            for (Marker markerInEvent : markerListInEvent) {
                if (markerInEvent.contains(markerStr)) {
                    return true;
                }
            }
        }
        return false;
    }
}
OnMarkerEvaluator继承了EventEvaluatorBase,其evaluate从ILoggingEvent获取markerListInEvent,然后判断markerListInEvent是否有包含指定的markerStr

OnErrorEvaluator

ch/qos/logback/classic/boolex/OnErrorEvaluator.java

public class OnErrorEvaluator extends EventEvaluatorBase<ILoggingEvent> {

    /**
     * Return true if event passed as parameter has level ERROR or higher, returns
     * false otherwise.
     */
    public boolean evaluate(ILoggingEvent event) throws NullPointerException, EvaluationException {
        return event.getLevel().levelInt >= Level.ERROR_INT;
    }
}
OnErrorEvaluator继承了EventEvaluatorBase,其evaluate方法根据event的level来判断看是否大于等于ERROR级别

JaninoEventEvaluatorBase

ch/qos/logback/core/boolex/JaninoEventEvaluatorBase.java

abstract public class JaninoEventEvaluatorBase<E> extends EventEvaluatorBase<E> {

    static Class<?> EXPRESSION_TYPE = boolean.class;
    static Class<?>[] THROWN_EXCEPTIONS = new Class[1];

    static public final int ERROR_THRESHOLD = 4;
    static {
        THROWN_EXCEPTIONS[0] = EvaluationException.class;
    }

    private String expression;

    ScriptEvaluator scriptEvaluator;
    private int errorCount = 0;

    public void start() {
        try {
            assert context != null;
            scriptEvaluator = new ScriptEvaluator(getDecoratedExpression(), EXPRESSION_TYPE, getParameterNames(),
                    getParameterTypes(), THROWN_EXCEPTIONS);
            super.start();
        } catch (Exception e) {
            addError("Could not start evaluator with expression [" + expression + "]", e);
        }
    }

    public boolean evaluate(E event) throws EvaluationException {
        if (!isStarted()) {
            throw new IllegalStateException("Evaluator [" + name + "] was called in stopped state");
        }
        try {
            Boolean result = (Boolean) scriptEvaluator.evaluate(getParameterValues(event));
            return result;
        } catch (Exception ex) {
            errorCount++;
            if (errorCount >= ERROR_THRESHOLD) {
                stop();
            }
            throw new EvaluationException("Evaluator [" + name + "] caused an exception", ex);
        }
    }

    //......
}            
JaninoEventEvaluatorBase在start的时候先通过getDecoratedExpression获取修饰之后的表达式,然后创建ScriptEvaluator,evaluate方法则通过getParameterValues从event提取参数,然后通过scriptEvaluator.evaluate获取结果

示例

            <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
              <evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
                  <marker>consoleOnly</marker>
              </evaluator>
              <onMatch>DENY</onMatch>
            </filter>
这里配置了EvaluatorFilter,其evaluator为OnMarkerEvaluator,包含consoleOnly这个marker时返回true,命中返回DENY

小结

logback的EvaluatorFilter继承了AbstractMatcherFilter,其decide方法在evaluator.evaluate(event)为true时返回onMatch,否则返回onMismatch,异常的话返回NEUTRAL。evaluator主要有OnMarkerEvaluator、OnErrorEvaluator以及JaninoEventEvaluatorBase系列的evaluator;JaninoEventEvaluatorBase它可以定义expression,通过ScriptEvaluator进行evaluate。


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...