• 聊聊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

  • 相关阅读:
    2023华为杯E题:出血性脑卒中临床智能诊疗建模(不断更新)
    2022.08.19 java web学习
    神经网络(二)回归与线性模型
    Linux 应急响应命令总结,收藏版
    【云原生&微服务十三】SpringCloud之深度源码剖析OpenFeign如何扫描所有的FeignClient
    Spring Cloud项目(七)——使用sentinel作为熔断降级
    WebVR — 网络虚拟现实
    win10 配置VSCode 的C++环境
    TableView (cocos-2dx lua)
    kubernetes(K8S)学习笔记P2:搭建K8s集群2种方式
  • 原文地址:https://blog.csdn.net/hello_ejb3/article/details/134239526