最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

logback?EvaluatorFilter日志過濾器源碼解讀

 更新時間:2023年11月17日 11:20:26   作者:codecraft  
這篇文章主要為大家介紹了logback?EvaluatorFilter日志過濾器源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

本文主要研究一下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聲明實現(xiàn)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方法根據(jù)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獲取修飾之后的表達(dá)式,然后創(chuàng)建ScriptEvaluator,evaluate方法則通過getParameterValues從event提取參數(shù),然后通過scriptEvaluator.evaluate獲取結(jié)果

示例

<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

小結(jié)

logback的EvaluatorFilter繼承了AbstractMatcherFilter,其decide方法在evaluator.evaluate(event)為true時返回onMatch,否則返回onMismatch,異常的話返回NEUTRAL。evaluator主要有OnMarkerEvaluator、OnErrorEvaluator以及JaninoEventEvaluatorBase系列的evaluator;JaninoEventEvaluatorBase它可以定義expression,通過ScriptEvaluator進(jìn)行evaluate。

以上就是logback EvaluatorFilter日志過濾器源碼解讀的詳細(xì)內(nèi)容,更多關(guān)于logback EvaluatorFilter日志過濾器的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 基于Java制作一個燈謎猜猜樂游戲

    基于Java制作一個燈謎猜猜樂游戲

    中秋佳節(jié),是我國傳統(tǒng)的重大節(jié)日之一,全國各地為了增強(qiáng)過節(jié)的氣氛,都有許多傳統(tǒng)的中秋活動,比如猜燈謎,所以本文就來用Java制作一個燈謎猜猜樂游戲,感興趣的可以了解下
    2023-09-09
  • Spring @value和@PropertySource注解使用方法解析

    Spring @value和@PropertySource注解使用方法解析

    這篇文章主要介紹了Spring @value和@PropertySource注解使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • SpringBoot整合activemq的案例代碼

    SpringBoot整合activemq的案例代碼

    ActiveMQ是消息隊列技術(shù),為解決高并發(fā)問題而生,本文通過案例代碼給大家介紹pringBoot整合activemq的詳細(xì)過程,感興趣的朋友跟隨小編一起看看吧
    2022-02-02
  • Java實現(xiàn)非阻塞式服務(wù)器的示例代碼

    Java實現(xiàn)非阻塞式服務(wù)器的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用Java實現(xiàn)一個簡單的非阻塞式服務(wù)器,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,需要的可以參考一下
    2023-05-05
  • java集合中HashSet LinkedHashSet TreeSet三者異同面試精講

    java集合中HashSet LinkedHashSet TreeSet三者異同面試精講

    這篇文章主要為大家介紹了java集合中HashSet LinkedHashSet TreeSet三者異同面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • gradle配置國內(nèi)鏡像的實現(xiàn)

    gradle配置國內(nèi)鏡像的實現(xiàn)

    這篇文章主要介紹了gradle配置國內(nèi)鏡像的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 在Spring環(huán)境中正確關(guān)閉線程池的姿勢

    在Spring環(huán)境中正確關(guān)閉線程池的姿勢

    這篇文章主要介紹了在Spring環(huán)境中正確關(guān)閉線程池的姿勢,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Java內(nèi)存模型原子性原理及實例解析

    Java內(nèi)存模型原子性原理及實例解析

    這篇文章主要介紹了Java內(nèi)存模型原子性原理及實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • 在 Spring Boot 中使用 @Autowired和 @Bean注解的示例詳解

    在 Spring Boot 中使用 @Autowired和 @Bean

    本文通過一個示例演示了如何在SpringBoot中使用@Autowired和@Bean注解進(jìn)行依賴注入和Bean管理,示例中定義了一個Student類,并通過配置類TestConfig初始化Student對象,在測試類中,通過@Autowired注解自動注入Student對象并輸出其屬性值,感興趣的朋友跟隨小編一起看看吧
    2025-02-02
  • spring boot mybatis多數(shù)據(jù)源解決方案過程解析

    spring boot mybatis多數(shù)據(jù)源解決方案過程解析

    這篇文章主要介紹了spring boot mybatis多數(shù)據(jù)源解決方案過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11

最新評論

黔江区| 前郭尔| 潍坊市| 墨玉县| 水富县| 邳州市| 襄城县| 安庆市| 济阳县| 绍兴县| 科尔| 新河县| 白城市| 吴川市| 农安县| 龙口市| 林西县| 青田县| 锡林浩特市| 庆城县| 高陵县| 镇江市| 封开县| 双柏县| 塘沽区| 莒南县| 苏州市| 洪江市| 扎鲁特旗| 南雄市| 思南县| 台州市| 新泰市| 慈利县| 道真| 乳山市| 澄江县| 庄浪县| 建水县| 江西省| 朔州市|