类图
前面代码看出来 最终返回的是LoggerContext这个类实现了LogFactory
成员变量
//表示根logger final Logger root = new Logger("ROOT", (Logger)null, this); //标识创建了多少个logger private int size; //未知 private int noAppenderWarning = 0; //logger监听器 private final List<LoggerContextListener> loggerContextListenerList = new ArrayList(); //logger缓存 get一次会存入缓存同时size++下次直接从缓存拿 private Map<String, Logger> loggerCache = new ConcurrentHashMap(); //loggerContextVO对象 private LoggerContextVO loggerContextRemoteView = new LoggerContextVO(this); private final TurboFilterList turboFilterList = new TurboFilterList(); private boolean packagingDataEnabled = false; private int maxCallerDataDepth = 8; //记录LoggerContext调用了几次方法 int resetCount = 0; private List<String> frameworkPackages;
LoggerContext
public static Logger getLogger(String name) { ILoggerFactory iLoggerFactory = getILoggerFactory(); return iLoggerFactory.getLogger(name); }
这里的LoggerFactory就是LoggerContext
return StaticLoggerBinder.getSingleton().getLoggerFactory();
private LoggerContext defaultLoggerContext = new LoggerContext(); public ILoggerFactory getLoggerFactory() { if (!this.initialized) { return this.defaultLoggerContext; } else if (this.contextSelectorBinder.getContextSelector() == null) { throw new IllegalStateException("contextSelector cannot be null. See also http://logback.qos.ch/codes.html#null_CS"); } else { return this.contextSelectorBinder.getContextSelector().getLoggerContext(); } }
getLogger
ch.qos.logback.classic.LoggerContext#getLogger(java.lang.String)
public final Logger getLogger(String name) { //如果name为空抛出异常 正常不会出现 if (name == null) { throw new IllegalArgumentException("name argument cannot be null"); //如果传入的是root直接返回root Logger } else if ("ROOT".equalsIgnoreCase(name)) { return this.root; } else { int i = 0; Logger logger = this.root; //从缓存中获取Logger如果没有则 Logger childLogger = (Logger)this.loggerCache.get(name); if (childLogger != null) { return childLogger; } else { int h; do { //<1>这里根据,号分割依次连续截取 h = LoggerNameUtil.getSeparatorIndexOf(name, i); String childName; if (h == -1) { childName = name; } else { childName = name.substring(0, h); } i = h + 1; synchronized(logger) { //<2>会维护父子关系 childLogger = logger.getChildByName(childName); if (childLogger == null) { childLogger = logger.createChildByName(childName); this.loggerCache.put(childName, childLogger); //每次创建count++ this.incSize(); } } logger = childLogger; } while(h != -1); return childLogger; } } }
<1>处会依次截取创建logger并放入cache 同时维护父子关系
cache数据