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

logback標(biāo)記日志過濾器MarkerFilter源碼解讀

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

本文主要研究一下logback的MarkerFilter

MarkerFilter

ch/qos/logback/classic/turbo/MarkerFilter.java

public class MarkerFilter extends MatchingFilter {
    Marker markerToMatch;
    @Override
    public void start() {
        if (markerToMatch != null) {
            super.start();
        } else {
            addError("The marker property must be set for [" + getName() + "]");
        }
    }
    @Override
    public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
        if (!isStarted()) {
            return FilterReply.NEUTRAL;
        }
        if (marker == null) {
            return onMismatch;
        }
        if (marker.contains(markerToMatch)) {
            return onMatch;
        } else {
            return onMismatch;
        }
    }
    /**
     * The marker to match in the event.
     * 
     * @param markerStr
     */
    public void setMarker(String markerStr) {
        if (markerStr != null) {
            this.markerToMatch = MarkerFactory.getMarker(markerStr);
        }
    }
}
MarkerFilter定義了markerToMatch屬性,其decide方法對(duì)于傳入的marker為null返回onMismatch,對(duì)于包含markerToMatch的返回onMatch,否則返回onMismatch

MarkerFactory

org/slf4j/MarkerFactory.java

public class MarkerFactory {
    static IMarkerFactory MARKER_FACTORY;
    private MarkerFactory() {
    }
    // this is where the binding happens
    static {
        SLF4JServiceProvider provider = LoggerFactory.getProvider();
        if (provider != null) {
            MARKER_FACTORY = provider.getMarkerFactory();
        } else {
            Util.report("Failed to find provider");
            Util.report("Defaulting to BasicMarkerFactory.");
            MARKER_FACTORY = new BasicMarkerFactory();
        }
    }
    /**
     * Return a Marker instance as specified by the name parameter using the
     * previously bound {@link IMarkerFactory}instance.
     * 
     * @param name
     *          The name of the {@link Marker} object to return.
     * @return marker
     */
    public static Marker getMarker(String name) {
        return MARKER_FACTORY.getMarker(name);
    }
    /**
     * Create a marker which is detached (even at birth) from the MarkerFactory.
     *
     * @param name the name of the marker
     * @return a dangling marker
     * @since 1.5.1
     */
    public static Marker getDetachedMarker(String name) {
        return MARKER_FACTORY.getDetachedMarker(name);
    }
    /**
     * Return the {@link IMarkerFactory}instance in use.
     * 
     * <p>The IMarkerFactory instance is usually bound with this class at 
     * compile time.
     * 
     * @return the IMarkerFactory instance in use
     */
    public static IMarkerFactory getIMarkerFactory() {
        return MARKER_FACTORY;
    }
}
MarkerFactory通過static方法來初始化MARKER_FACTORY,若SLF4JServiceProvider不為null則取provider.getMarkerFactory(),否則取BasicMarkerFactory

BasicMarkerFactory

org/slf4j/helpers/BasicMarkerFactory.java

public class BasicMarkerFactory implements IMarkerFactory {
    private final ConcurrentMap<String, Marker> markerMap = new ConcurrentHashMap<>();
    /**
     * Regular users should <em>not</em> create
     * <code>BasicMarkerFactory</code> instances. <code>Marker</code>
     * instances can be obtained using the static {@link
     * org.slf4j.MarkerFactory#getMarker} method.
     */
    public BasicMarkerFactory() {
    }
    /**
     * Manufacture a {@link BasicMarker} instance by name. If the instance has been 
     * created earlier, return the previously created instance. 
     * 
     * @param name the name of the marker to be created
     * @return a Marker instance
     */
    public Marker getMarker(String name) {
        if (name == null) {
            throw new IllegalArgumentException("Marker name cannot be null");
        }
        Marker marker = markerMap.get(name);
        if (marker == null) {
            marker = new BasicMarker(name);
            Marker oldMarker = markerMap.putIfAbsent(name, marker);
            if (oldMarker != null) {
                marker = oldMarker;
            }
        }
        return marker;
    }
    /**
     * Does the name marked already exist?
     */
    public boolean exists(String name) {
        if (name == null) {
            return false;
        }
        return markerMap.containsKey(name);
    }
    public boolean detachMarker(String name) {
        if (name == null) {
            return false;
        }
        return (markerMap.remove(name) != null);
    }
    public Marker getDetachedMarker(String name) {
        return new BasicMarker(name);
    }
}
BasicMarkerFactory通過ConcurrentMap來存儲(chǔ)string與Marker的映射,創(chuàng)建的是BasicMarker

Marker

org/slf4j/Marker.java

public interface Marker extends Serializable {
    /**
     * This constant represents any marker, including a null marker.
     */
    public final String ANY_MARKER = "*";
    /**
     * This constant represents any non-null marker.
     */
    public final String ANY_NON_NULL_MARKER = "+";
    /**
     * Get the name of this Marker.
     * 
     * @return name of marker
     */
    public String getName();
    /**
     * Add a reference to another Marker.
     *
     * <p>Note that the fluent API allows adding multiple markers to a logging statement.
     * It is often preferable to use multiple markers instead of nested markers.
     * </p>
     *
     * @param reference
     *                a reference to another marker
     * @throws IllegalArgumentException
     *                 if 'reference' is null
     */
    public void add(Marker reference);
    /**
     * Remove a marker reference.
     * 
     * @param reference
     *                the marker reference to remove
     * @return true if reference could be found and removed, false otherwise.
     */
    public boolean remove(Marker reference);
    /**
     * @deprecated Replaced by {@link #hasReferences()}.
     */
    @Deprecated
    public boolean hasChildren();
    /**
     * Does this marker have any references?
     * 
     * @return true if this marker has one or more references, false otherwise.
     */
    public boolean hasReferences();
    /**
     * Returns an Iterator which can be used to iterate over the references of this
     * marker. An empty iterator is returned when this marker has no references.
     * 
     * @return Iterator over the references of this marker
     */
    public Iterator<Marker> iterator();
    /**
     * Does this marker contain a reference to the 'other' marker? Marker A is defined 
     * to contain marker B, if A == B or if B is referenced by A, or if B is referenced
     * by any one of A's references (recursively).
     * 
     * @param other
     *                The marker to test for inclusion.
     * @throws IllegalArgumentException
     *                 if 'other' is null
     * @return Whether this marker contains the other marker.
     */
    public boolean contains(Marker other);
    /**
     * Does this marker contain the marker named 'name'?
     * 
     * If 'name' is null the returned value is always false.
     * 
     * @param name The marker name to test for inclusion.
     * @return Whether this marker contains the other marker.
     */
    public boolean contains(String name);
    /**
     * Markers are considered equal if they have the same name.
     *
     * @param o
     * @return true, if this.name equals o.name
     *
     * @since 1.5.1
     */
    public boolean equals(Object o);
    /**
     * Compute the hash code based on the name of this marker.
     * Note that markers are considered equal if they have the same name.
     * 
     * @return the computed hashCode
     * @since 1.5.1
     */
    public int hashCode();
}
Marker接口定義了getName、add、remove、hasReferences、iterator、contains、equals、hashCode方法

BasicMarker

org/slf4j/helpers/BasicMarker.java

public class BasicMarker implements Marker {
    private static final long serialVersionUID = -2849567615646933777L;
    private final String name;
    private final List<Marker> referenceList = new CopyOnWriteArrayList<>();
    BasicMarker(String name) {
        if (name == null) {
            throw new IllegalArgumentException("A marker name cannot be null");
        }
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void add(Marker reference) {
        if (reference == null) {
            throw new IllegalArgumentException("A null value cannot be added to a Marker as reference.");
        }
        // no point in adding the reference multiple times
        if (this.contains(reference)) {
            return;
        } else if (reference.contains(this)) { // avoid recursion
            // a potential reference should not hold its future "parent" as a reference
            return;
        } else {
            referenceList.add(reference);
        }
    }
    public boolean hasReferences() {
        return (referenceList.size() > 0);
    }
    @Deprecated
    public boolean hasChildren() {
        return hasReferences();
    }
    public Iterator<Marker> iterator() {
        return referenceList.iterator();
    }
    public boolean remove(Marker referenceToRemove) {
        return referenceList.remove(referenceToRemove);
    }
    public boolean contains(Marker other) {
        if (other == null) {
            throw new IllegalArgumentException("Other cannot be null");
        }
        if (this.equals(other)) {
            return true;
        }
        if (hasReferences()) {
            for (Marker ref : referenceList) {
                if (ref.contains(other)) {
                    return true;
                }
            }
        }
        return false;
    }
    /**
     * This method is mainly used with Expression Evaluators.
     */
    public boolean contains(String name) {
        if (name == null) {
            throw new IllegalArgumentException("Other cannot be null");
        }
        if (this.name.equals(name)) {
            return true;
        }
        if (hasReferences()) {
            for (Marker ref : referenceList) {
                if (ref.contains(name)) {
                    return true;
                }
            }
        }
        return false;
    }
    private static final String OPEN = "[ ";
    private static final String CLOSE = " ]";
    private static final String SEP = ", ";
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Marker))
            return false;
        final Marker other = (Marker) obj;
        return name.equals(other.getName());
    }
    public int hashCode() {
        return name.hashCode();
    }
    public String toString() {
        if (!this.hasReferences()) {
            return this.getName();
        }
        Iterator<Marker> it = this.iterator();
        Marker reference;
        StringBuilder sb = new StringBuilder(this.getName());
        sb.append(' ').append(OPEN);
        while (it.hasNext()) {
            reference = it.next();
            sb.append(reference.getName());
            if (it.hasNext()) {
                sb.append(SEP);
            }
        }
        sb.append(CLOSE);
        return sb.toString();
    }
}
BasicMarker實(shí)現(xiàn)了Marker接口,它定義了一個(gè)CopyOnWriteArrayList類型的referenceList

示例

配置

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
  <turboFilter class="ch.qos.logback.classic.turbo.MDCFilter">
    <MDCKey>username</MDCKey>
    <Value>sebastien</Value>
    <OnMatch>ACCEPT</OnMatch>
  </turboFilter>
  <turboFilter class="ch.qos.logback.classic.turbo.MarkerFilter">
    <Marker>billing</Marker>
    <OnMatch>DENY</OnMatch>
  </turboFilter>
  <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%date [%thread] %-5level %logger - %msg%n</Pattern>
    </layout>
  </appender>
  <root level="info">
    <appender-ref ref="console" />
  </root>
</configuration>

使用

Marker billing = MarkerFactory.getMarker("billing");
logger.error(billing, "billing statement {}", i);

小結(jié)

logback提供了MarkerFilter來支持slf4j的Marker,可以通過MarkerFactory.getMarker獲取marker,然后在logger中使用,而配置文件可以配置MarkerFilter,設(shè)置指定的marker的onMatch或者onMismatch行為。

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

相關(guān)文章

  • Java使用EasyExcel動(dòng)態(tài)添加自增序號(hào)列

    Java使用EasyExcel動(dòng)態(tài)添加自增序號(hào)列

    本文將介紹如何通過使用EasyExcel自定義攔截器實(shí)現(xiàn)在最終的Excel文件中新增一列自增的序號(hào)列,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • SpringBoot應(yīng)用啟動(dòng)過程分析

    SpringBoot應(yīng)用啟動(dòng)過程分析

    這篇文章主要介紹了SpringBoot應(yīng)用啟動(dòng)過程分析,需要的朋友可以參考下
    2017-08-08
  • spring+mybatis實(shí)現(xiàn)圖書管理系統(tǒng)

    spring+mybatis實(shí)現(xiàn)圖書管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了spring+mybatis實(shí)現(xiàn)圖書管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • 一文詳解Java抽象類到底有多抽象

    一文詳解Java抽象類到底有多抽象

    這篇文章主要介紹了一文詳解Java抽象類到底有多抽象,抽象方法所在的類必須是抽象類,子類若繼承了一個(gè)抽象類,就必須覆寫父類的所有抽象方法,這里的子類是普通類,是強(qiáng)制要求覆寫所有抽象方法,但是如果子類也是一個(gè)抽象類,那么就可以不覆寫
    2022-06-06
  • 為何Java8需要引入新的日期與時(shí)間庫(kù)

    為何Java8需要引入新的日期與時(shí)間庫(kù)

    這篇文章主要給大家介紹了關(guān)于Java8為什么需要引入新的日期與時(shí)間庫(kù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Netty與Spring Boot的整合實(shí)現(xiàn)

    Netty與Spring Boot的整合實(shí)現(xiàn)

    這篇文章主要介紹了Netty與Spring Boot的整合的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • spring注解識(shí)別一個(gè)接口的多個(gè)實(shí)現(xiàn)類方法

    spring注解識(shí)別一個(gè)接口的多個(gè)實(shí)現(xiàn)類方法

    下面小編就為大家?guī)硪黄猻pring注解識(shí)別一個(gè)接口的多個(gè)實(shí)現(xiàn)類方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • Apache commons fileupload文件上傳實(shí)例講解

    Apache commons fileupload文件上傳實(shí)例講解

    這篇文章主要為大家詳細(xì)介紹了Apache commons fileupload文件上傳實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • spring security在分布式項(xiàng)目下的配置方法(案例詳解)

    spring security在分布式項(xiàng)目下的配置方法(案例詳解)

    這篇文章主要介紹了spring security在分布式項(xiàng)目下的配置方法,本文通過一個(gè)項(xiàng)目案例給大家詳細(xì)介紹,通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • Java?Swing監(jiān)聽器的原理及使用方法舉例

    Java?Swing監(jiān)聽器的原理及使用方法舉例

    swing是一個(gè)比較老的技術(shù)了,我覺得學(xué)習(xí)它還是很有必要的,也比較容易激發(fā)學(xué)習(xí)的興趣,這篇文章主要介紹了Java?Swing監(jiān)聽器的原理及使用方法的相關(guān)資料,需要的朋友可以參考下
    2025-08-08

最新評(píng)論

乐清市| 聊城市| 儋州市| 淳安县| 迁西县| 宜宾县| 宁明县| 平度市| 体育| 白银市| 马公市| 武穴市| 南陵县| 阳原县| 四平市| 许昌县| 综艺| 万安县| 武冈市| 读书| 舒兰市| 闵行区| 定州市| 襄垣县| 开原市| 台东市| 太湖县| 长宁区| 乌审旗| 西贡区| 太仆寺旗| 伊春市| 南涧| 永济市| 崇州市| 卢湾区| 白银市| 上饶市| 轮台县| 吴旗县| 兴业县|