logback高效状态管理器StatusManager源码解析

 更新时间:2023年11月07日 09:49:35   作者:codecraft  
这篇文章主要为大家介绍了logback高效状态管理器StatusManager源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

本文主要研究一下logback的StatusManager

StatusManager

ch/qos/logback/core/status/StatusManager.java

public interface StatusManager {
    /**
     * Add a new status message.
     * 
     * @param status
     */
    void add(Status status);
    /**
     * Obtain a copy of the status list maintained by this StatusManager.
     * 
     * @return
     */
    List<Status> getCopyOfStatusList();
    /**
     * Return the highest level of all the statii.
     * 
     * @return
     */
    // int getLevel();
    /**
     * Return the number of status entries.
     * 
     * @return
     */
    int getCount();
    /**
     * Add a status listener.
     * 
     * @param listener
     */
    /**
     * Add a status listener. The StatusManager may decide to skip installation if
     * an earlier instance was already installed.
     * 
     * @param listener
     * @return true if actually added, false if skipped
     */
    boolean add(StatusListener listener);
    /**
     * ); Remove a status listener.
     * 
     * @param listener
     */
    void remove(StatusListener listener);
    /**
     * Clear the list of status messages.
     */
    void clear();
    /**
     * Obtain a copy of the status listener list maintained by this StatusManager
     * 
     * @return
     */
    List<StatusListener> getCopyOfStatusListenerList();
}
StatusManager接口针对status定义了add、getCopyOfStatusList、getCount、clear方法,针对StatusListener定义了add、remove、getCopyOfStatusListenerList方法

BasicStatusManager

ch/qos/logback/core/BasicStatusManager.java

public class BasicStatusManager implements StatusManager {
    final protected List<Status> statusList = new ArrayList<Status>();
    final protected CyclicBuffer<Status> tailBuffer = new CyclicBuffer<Status>(TAIL_SIZE);
    final protected LogbackLock statusListLock = new LogbackLock();
    final protected List<StatusListener> statusListenerList = new ArrayList<StatusListener>();
    final protected LogbackLock statusListenerListLock = new LogbackLock();
    //......
}
BasicStatusManager实现了StatusManager接口,它使用statusList及statusListLock来操作status,使用statusListenerList及statusListenerListLock来操作StatusListener;另外针对status还提供了tailBuffer

add status

public void add(Status newStatus) {
        // LBCORE-72: fire event before the count check
        fireStatusAddEvent(newStatus);
        count++;
        if (newStatus.getLevel() > level) {
            level = newStatus.getLevel();
        }
        synchronized (statusListLock) {
            if (statusList.size() < MAX_HEADER_COUNT) {
                statusList.add(newStatus);
            } else {
                tailBuffer.add(newStatus);
            }
        }
    }
add方法先加锁,再判断是否超出限制,没有则添加到statusList,超出则添加到tailBuffer

getCopyOfStatusList

public List<Status> getCopyOfStatusList() {
        synchronized (statusListLock) {
            List<Status> tList = new ArrayList<Status>(statusList);
            tList.addAll(tailBuffer.asList());
            return tList;
        }
    }
getCopyOfStatusList则加锁,然后从statusList及tailBuffer获取status

clear

public void clear() {
        synchronized (statusListLock) {
            count = 0;
            statusList.clear();
            tailBuffer.clear();
        }
    }
clear则加锁,重置count,清空statusList及tailBuffer

add listener

public boolean add(StatusListener listener) {
        synchronized (statusListenerListLock) {
            if (listener instanceof OnConsoleStatusListener) {
                boolean alreadyPresent = checkForPresence(statusListenerList, listener.getClass());
                if (alreadyPresent)
                    return false;
            }
            statusListenerList.add(listener);
        }
        return true;
    }
    private boolean checkForPresence(List<StatusListener> statusListenerList, Class<?> aClass) {
        for (StatusListener e : statusListenerList) {
            if (e.getClass() == aClass)
                return true;
        }
        return false;
    }
add listener方法先加锁,然后判断是否已经存在,不存在则添加到statusListenerList

remove

public void remove(StatusListener listener) {
        synchronized (statusListenerListLock) {
            statusListenerList.remove(listener);
        }
    }
remove则先加锁,然后从statusListenerList中移除

getCopyOfStatusListenerList

public List<StatusListener> getCopyOfStatusListenerList() {
        synchronized (statusListenerListLock) {
            return new ArrayList<StatusListener>(statusListenerList);
        }
    }
getCopyOfStatusListenerList则先加锁然后拷贝statusListenerList

Status

ch/qos/logback/core/status/Status.java

public interface Status {
    int INFO = 0;
    int WARN = 1;
    int ERROR = 2;
    int getLevel();
    int getEffectiveLevel();
    Object getOrigin();
    String getMessage();
    Throwable getThrowable();
    /**
     * @eprecated. Use getTimestamp instead.
     * @return
     */
    @Deprecated
    default Long getDate() {
        return getTimestamp();
    }
    long getTimestamp();
    boolean hasChildren();
    void add(Status child);
    boolean remove(Status child);
    Iterator<Status> iterator();
}
Status接口定义了getLevel、getEffectiveLevel、getOrigin、getMessage、getThrowable、getTimestamp、hasChildren、add、remove、iterator方法

StatusBase

ch/qos/logback/core/status/StatusBase.java

abstract public class StatusBase implements Status {
    static private final List<Status> EMPTY_LIST = new ArrayList<Status>(0);
    int level;
    final String message;
    final Object origin;
    List<Status> childrenList;
    Throwable throwable;
    long timestamp;
    public synchronized void add(Status child) {
        if (child == null) {
            throw new NullPointerException("Null values are not valid Status.");
        }
        if (childrenList == null) {
            childrenList = new ArrayList<Status>();
        }
        childrenList.add(child);
    }
    public synchronized boolean hasChildren() {
        return ((childrenList != null) && (childrenList.size() > 0));
    }
    public synchronized Iterator<Status> iterator() {
        if (childrenList != null) {
            return childrenList.iterator();
        } else {
            return EMPTY_LIST.iterator();
        }
    }
    public synchronized boolean remove(Status statusToRemove) {
        if (childrenList == null) {
            return false;
        }
        // TODO also search in childrens' children
        return childrenList.remove(statusToRemove);
    }
    public synchronized int getEffectiveLevel() {
        int result = level;
        int effLevel;
        Iterator<Status> it = iterator();
        Status s;
        while (it.hasNext()) {
            s = (Status) it.next();
            effLevel = s.getEffectiveLevel();
            if (effLevel > result) {
                result = effLevel;
            }
        }
        return result;
    }
    //......
}
StatusBase声明实现Status接口,它通过childrenList来存储子status

InfoStatus

ch/qos/logback/core/status/InfoStatus.java

public class InfoStatus extends StatusBase {
    public InfoStatus(String msg, Object origin) {
        super(Status.INFO, msg, origin);
    }
    public InfoStatus(String msg, Object origin, Throwable t) {
        super(Status.INFO, msg, origin, t);
    }
}
InfoStatus继承了StatusBase,它的level为Status.INFO

WarnStatus

ch/qos/logback/core/status/WarnStatus.java

public class WarnStatus extends StatusBase {
    public WarnStatus(String msg, Object origin) {
        super(Status.WARN, msg, origin);
    }
    public WarnStatus(String msg, Object origin, Throwable t) {
        super(Status.WARN, msg, origin, t);
    }
}
WarnStatus继承了StatusBase,它的level为Status.WARN

ErrorStatus

ch/qos/logback/core/status/ErrorStatus.java

public class ErrorStatus extends StatusBase {
    public ErrorStatus(String msg, Object origin) {
        super(Status.ERROR, msg, origin);
    }
    public ErrorStatus(String msg, Object origin, Throwable t) {
        super(Status.ERROR, msg, origin, t);
    }
}
ErrorStatus继承了StatusBase,它的level为Status.ERROR

小结

logback定义了StatusManager用于管理status及其listener,其add方法会回调listener,之后加锁,再判断是否超出限制,没有则添加到statusList,超出则添加到tailBuffer;Status是个接口,它有一个抽象类为StatusBase,而InfoStatus、WarnStatus、ErrorStatus都继承了StatusBase。

以上就是logback高效状态管理器StatusManager源码解析的详细内容,更多关于logback StatusManager状态管理的资料请关注脚本之家其它相关文章!

相关文章

  • SpringBoot 如何读取pom.xml中的值

    SpringBoot 如何读取pom.xml中的值

    这篇文章主要介绍了SpringBoot 如何读取pom.xml中的值,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Javaweb实现邮件发送

    Javaweb实现邮件发送

    这篇文章主要为大家详细介绍了Javaweb实现邮件发送,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • JVM类加载机制详解

    JVM类加载机制详解

    本文主要介绍了JVM类加载机制的相关知识,具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • java实现ftp文件上传下载功能

    java实现ftp文件上传下载功能

    这篇文章主要为大家详细介绍了java实现ftp文件上传下载功能的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • mybatis-plus批量更新太慢该如何解决详解

    mybatis-plus批量更新太慢该如何解决详解

    这篇文章主要给大家介绍了关于mybatis-plus批量更新太慢该如何解决的相关资料,文中通过实例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2023-03-03
  • Spring事务传播中嵌套调用实现方法详细介绍

    Spring事务传播中嵌套调用实现方法详细介绍

    Spring事务的本质就是对数据库事务的支持,没有数据库事务,Spring是无法提供事务功能的。Spring只提供统一的事务管理接口,具体实现都是由数据库自己实现的,Spring会在事务开始时,根据当前设置的隔离级别,调整数据库的隔离级别,由此保持一致
    2022-11-11
  • Java HashTable的原理与实现

    Java HashTable的原理与实现

    Java中的HashTable是一种线程安全的哈希表实现,它可以高效地存储和快速查找数据,本文将介绍Java中的HashTable的实现原理、常用方法和测试用例,需要的小伙伴可以参考一下
    2023-09-09
  • Hadoop源码分析四远程debug调试

    Hadoop源码分析四远程debug调试

    本篇是Hadoop源码分析系列文章第四篇,主要介绍一下Hadoop的远程debug调试步骤,后续本系列文章会持续更新,有需要的朋友可以借鉴参考下
    2021-09-09
  • 浅析Java中的异常处理机制

    浅析Java中的异常处理机制

    这篇文章主要介绍了Java中的异常处理机制的相关资料,帮助大家更好的理解和学习Java,感兴趣的朋友可以了解下
    2020-11-11
  • Spring中filter过滤器的定义方法

    Spring中filter过滤器的定义方法

    这篇文章主要介绍了Spring中filter过滤器的定义方法,Filter 程序是一个实现了特殊接口的 Java 类,与 Servlet 类似,也是由 Servlet 容器进行调用和执行的,需要的朋友可以参考下
    2023-08-08

最新评论