首页   快速返回

logback的内部日志输出     所属分类 java
logback1.2.3


public class ContextBase implements Context, LifeCycle
private StatusManager sm = new BasicStatusManager();

// Internal error messages
public interface StatusManager
   // Add a new status message.
   void add(Status status);
   boolean add(StatusListener listener);
   ...


BasicStatusManager
final protected List statusList = new ArrayList();
final protected CyclicBuffer tailBuffer = new CyclicBuffer(TAIL_SIZE);
final protected LogbackLock statusListLock = new LogbackLock();


   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);
               }
           }

       }



ch.qos.logback.core.status.OnConsoleStatusListener


public class OnConsoleStatusListener extends OnPrintStreamStatusListenerBase {
    @Override
    protected PrintStream getPrintStream() {
        return System.out;
    }
}


public void addStatusEvent(Status status) {
        if (!isStarted)
            return;
        print(status);
    }



BasicStatusManager 的使用



    private URL getResource(String filename, ClassLoader myClassLoader, boolean updateStatus) {
            URL url = Loader.getResource(filename, myClassLoader);
            // updateStatus 为 true , 写入 status 
            if (updateStatus) {
                statusOnResourceSearch(filename, myClassLoader, url);
            }
            return url;
    }


    private void statusOnResourceSearch(String resourceName, ClassLoader classLoader, URL url) {
            StatusManager sm = loggerContext.getStatusManager();
            if (url == null) {
                sm.add(new InfoStatus("Could NOT find resource [" + resourceName + "]", loggerContext));
            } else {
                sm.add(new InfoStatus("Found resource [" + resourceName + "] at [" + url.toString() + "]", loggerContext));
                multiplicityWarning(resourceName, classLoader);
            }
        }



08:55:51,997 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
08:55:51,997 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
08:55:51,998 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/Users/dugang/fun/hellocode/demo/springbootdemo/target/classes/logback.xml]

上一篇     下一篇
常用时间单位换算

slf4j简介

logback初始化过程

System.out重定向到slf4j

各种日志重定向到slf4j

ServiceLoader机制及实例