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

聊聊logback的EvaluatorFilter

本文主要研究一下logback的EvaluatorFilter

EvaluatorFilter

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

public class EvaluatorFilter<E> extends AbstractMatcherFilter<E> {EventEvaluator<E> evaluator;@Overridepublic 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.lryc.cn/news/235850.html

相关文章:

  • 解决vue 部分页面缓存,部分页面不缓存的问题
  • 修完这个 Bug 后,MySQL 性能提升了 300%
  • 【C/PTA】数组进阶练习(二)
  • Mysql MMM
  • GDPU 数据结构 天码行空10
  • CD36 ; + Lectin;
  • Git 分支管理
  • Vue23全局事件总线
  • GEM5 Garnet DVFS / NoC DVFS教程:ruby.clk_domain ruby.voltage_domain
  • java命令 jmap 堆参数分析
  • OpenCV C++ 图像处理实战 ——《OCR字符识别》
  • 在MySQL中创建新的数据库,可以使用命令,也可以通过MySQL工作台
  • 2311rust到31版本更新
  • 【Python百宝箱】视觉算法秀:Python图像处理舞台上的巅峰对决
  • Flutter 中在单个屏幕上实现多个列表
  • YOLOv8 加持 MobileNetv3,目标检测新篇章
  • .gitignore 文件——如何在 Git 中忽略文件和文件夹详细教程
  • 【数据结构(二)】单链表(3)
  • 创新案例|云服务平台HashiCorp是如何构建开源社区实现B2B增长飞轮
  • 2024年软件测试面试必看系列,看完去面试你会感谢我的!!
  • 01ctfer 文件上传
  • 2.2 调用星火大模型的API
  • 云原生是整个信息化行业的未来,一文彻底搞懂云原生
  • 【Redis】RedisTemplate最全的常用方法
  • 图像倾斜角度求取-Radon变换
  • 如何在本地搭建Oracle数据库实现公网环境下通过PLSQL工具进行远程访问
  • 时序预测 | Python实现ConvLSTM卷积长短期记忆神经网络股票价格预测(Conv1D-LSTM)
  • qtpdfium的编译及读取pdf文件和一些简单操作
  • ClickHouse查看执行计划
  • 2023-11-17 VsCode使用makefile进行多文件编译