当前位置: 首页 > article >正文

聊聊logback的EvaluatorFilter

本文主要研究一下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。


http://www.kler.cn/a/134095.html

相关文章:

  • 知识库管理系统:企业数字化转型的加速器
  • react动态路由
  • 深入理解 Vue v-model 原理与应用
  • 【vue2.0入门】vue单文件组件
  • 【css】html里面的图片宽度设为百分比,高度要与宽度一样
  • 【设计模式】关联关系与依赖关系
  • 计算机硬件的基本组成
  • K-Means聚类
  • 数电实验-----实现74LS139芯片扩展为3-8译码器以及应用(Quartus II )
  • 【VSCode】Visual Studio Code 下载与安装教程
  • macos 配置ndk环境
  • 【DevOps】Git 图文详解(四):Git 使用入门
  • 阿坤老师的独特瓷器(Java详解)
  • Linux下快速确定目标服务器支持哪些协议和密码套件
  • 学习网络编程No.10【深入学习HTTPS】
  • sqlite 判断数据表是否存在 失效的一种情况
  • Python数据分析实战① Python实现数据可视化
  • Unity中Shader法线贴图(上)
  • qt 重载信号,使用““方式进行connect()调用解决方案
  • 【算法与数据结构】前言
  • WPF中如何在MVVM模式下关闭窗口
  • 【0到1学习Unity脚本编程】第一人称视角的角色控制器
  • 技术贴 | SQL 执行 - 执行器优化
  • 【六袆 - MySQL】SQL优化;Explain SQL执行计划分析;
  • WPF位图效果
  • 详解ssh远程登录服务