• 聊聊logback的StatusListener


    本文主要研究一下logback的StatusListener

    StatusListener

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

    /**
     * A StatusListener registered with logback context's {@link StatusManager} will
     * receive notification of every incoming {@link Status status} message.
     * 
     * @author Ceki Gülcü
     */
    public interface StatusListener {
        void addStatusEvent(Status status);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    StatusListener定义了addStatusEvent方法,注册到logback上下文的StatusManager的StatusListener将接收每个传入状态消息

    NopStatusListener

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

    public class NopStatusListener implements StatusListener {
    
        public void addStatusEvent(Status status) {
            // nothing to do
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    NopStatusListener实现了StatusListener,它的addStatusEvent是空操作

    OnPrintStreamStatusListenerBase

    abstract public class OnPrintStreamStatusListenerBase extends ContextAwareBase implements StatusListener, LifeCycle {
    
        boolean isStarted = false;
    
        static final long DEFAULT_RETROSPECTIVE = 300;
        long retrospectiveThresold = DEFAULT_RETROSPECTIVE;
        
        /**
         * The prefix to place before each status message
         * @since 1.1.10
         */
        String prefix;
        
        /**
         * The PrintStream used by derived classes
         * @return
         */
        abstract protected PrintStream getPrintStream();
    
        public void addStatusEvent(Status status) {
            if (!isStarted)
                return;
            print(status);
        }
    
        private void print(Status status) {
            StringBuilder sb = new StringBuilder();
            if(prefix != null)
                sb.append(prefix);
            
            StatusPrinter.buildStr(sb, "", status);
            getPrintStream().print(sb);
        }
    
        //......
    }            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    OnPrintStreamStatusListenerBase声明实现了StatusListener接口,它是一个抽象类,其addStatusEvent方法执行print,而print则先添加prefix,再添加status,最后通过getPrintStream抽象方法获取PrintStream,然后进行print

    OnConsoleStatusListener

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

    /**
     * Print all new incoming status messages on the console (System.out).
     *
     * @author Ceki Gülcü
     */
    public class OnConsoleStatusListener extends OnPrintStreamStatusListenerBase {
        
        
        @Override
        protected PrintStream getPrintStream() {
            return System.out;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    OnConsoleStatusListener继承了OnPrintStreamStatusListenerBase,其getPrintStream返回的是System.out

    OnErrorConsoleStatusListener

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

    /**
     * Print all new incoming status messages on the error console (System.err).
     *
     * @author Ceki Gülcü
     * @since 1.0.8
     */
    public class OnErrorConsoleStatusListener extends OnPrintStreamStatusListenerBase {
    
        @Override
        protected PrintStream getPrintStream() {
            return System.err;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    OnErrorConsoleStatusListener继承了OnPrintStreamStatusListenerBase,其getPrintStream返回的是System.err

    StatusListenerAsList

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

    /**
     * Collect all incoming events in a list.
     * 
     * @author Ceki Gülcü
     * 
     */
    public class StatusListenerAsList implements StatusListener {
    
        List statusList = new ArrayList();
    
        public void addStatusEvent(Status status) {
            statusList.add(status);
        }
    
        public List getStatusList() {
            return statusList;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    StatusListenerAsList实现了StatusListener接口,其addStatusEvent将status添加到statusList

    小结

    logback的StatusListener用于打印logback内部的status信息,它有NopStatusListener实现及OnPrintStreamStatusListenerBase的实现(OnConsoleStatusListenerOnErrorConsoleStatusListener),即往System.out或者err打印。

    doc

  • 相关阅读:
    恶意代码防范技术笔记(八)
    2-分类问题 SVM 核函数
    基于STM32与FreeRTOS的消息传递详解(HAL库)
    latex图片编号+表格编号
    pwm呼吸灯
    Mybatis Plugin插件
    STM32F103VET6基于ENC28J60移植LWIP1.4.1(标准库,无RTOS)
    JNI 基础
    详细介绍NLP中文分词原理及分词工具
    帆软BI开发-Day2-趋势图的多种变形
  • 原文地址:https://blog.csdn.net/hello_ejb3/article/details/134239526