static Set<URL> findPossibleStaticLoggerBinderPathSet() { // use Set instead of list in order to deal with bug #138 // LinkedHashSet appropriate here because it preserves insertion order // during iteration Set<URL> staticLoggerBinderPathSet = newLinkedHashSet<URL>(); try { ClassLoaderloggerFactoryClassLoader= LoggerFactory.class.getClassLoader(); Enumeration<URL> paths; if (loggerFactoryClassLoader == null) { paths = ClassLoader.getSystemResources(STATIC_LOGGER_BINDER_PATH); } else { paths = loggerFactoryClassLoader.getResources(STATIC_LOGGER_BINDER_PATH); } while (paths.hasMoreElements()) { URLpath= paths.nextElement(); staticLoggerBinderPathSet.add(path); } } catch (IOException ioe) { Util.report("Error getting resources from path", ioe); } return staticLoggerBinderPathSet; }
publicvoidconfigureByResource(URL url)throws JoranException { if (url == null) { thrownewIllegalArgumentException("URL argument cannot be null"); } finalStringurlString= url.toString(); if (urlString.endsWith("groovy")) { if (EnvUtil.isGroovyAvailable()) { // avoid directly referring to GafferConfigurator so as to avoid // loading groovy.lang.GroovyObject . See also http://jira.qos.ch/browse/LBCLASSIC-214 GafferUtil.runGafferConfiguratorOn(loggerContext, this, url); } else { StatusManagersm= loggerContext.getStatusManager(); sm.add(newErrorStatus("Groovy classes are not available on the class path. ABORTING INITIALIZATION.", loggerContext)); } } elseif (urlString.endsWith("xml")) { JoranConfiguratorconfigurator=newJoranConfigurator(); configurator.setContext(loggerContext); configurator.doConfigure(url); } else { thrownewLogbackException("Unexpected filename extension of file [" + url.toString() + "]. Should be either .groovy or .xml"); } }
longthreshold= System.currentTimeMillis(); // if (!ConfigurationWatchListUtil.wasConfigurationWatchListReset(context)) { // informContextOfURLUsedForConfiguration(getContext(), null); // } SaxEventRecorderrecorder=newSaxEventRecorder(context); recorder.recordEvents(inputSource); doConfigure(recorder.saxEventList); // no exceptions a this level StatusUtilstatusUtil=newStatusUtil(context); if (statusUtil.noXMLParsingErrorsOccurred(threshold)) { addInfo("Registering current configuration as safe fallback point"); registerSafeConfiguration(recorder.saxEventList); } }
需要解析 xml 了,先是调用 buildInterpreter 构建了 Interpreter
1 2 3 4 5 6 7
publicvoiddoConfigure(final List<SaxEvent> eventList)throws JoranException { buildInterpreter(); // disallow simultaneous configurations of the same context synchronized (context.getConfigurationLock()) { interpreter.getEventPlayer().play(eventList); } }
// add jmxConfigurator only if we have JMX available. // If running under JDK 1.4 (retrotranslateed logback) then we // might not have JMX. if (PlatformInfo.hasJMXObjectName()) { rs.addRule(newElementSelector("configuration/jmxConfigurator"), newJMXConfiguratorAction()); } rs.addRule(newElementSelector("configuration/include"), newIncludeAction());
Iterator<Action> i = applicableActionList.iterator(); while (i.hasNext()) { Actionaction= (Action) i.next(); // now let us invoke the action. We catch and report any eventual // exceptions try { action.begin(interpretationContext, tagName, atts); } catch (ActionException e) { skip = elementPath.duplicate(); cai.addError("ActionException in Action for tag [" + tagName + "]", e); } catch (RuntimeException e) { skip = elementPath.duplicate(); cai.addError("RuntimeException in Action for tag [" + tagName + "]", e); } } }
publicvoidbegin(InterpretationContext ec, String name, Attributes attributes) { // Let us forget about previous errors (in this object) inError = false; logger = null;
if (OptionHelper.isEmpty(loggerName)) { inError = true; StringaroundLine= getLineColStr(ec); StringerrorMsg="No 'name' attribute in element " + name + ", around " + aroundLine; addError(errorMsg); return; }
if (name == null) { thrownewIllegalArgumentException("name argument cannot be null"); }
// if we are asking for the root logger, then let us return it without // wasting time if (Logger.ROOT_LOGGER_NAME.equalsIgnoreCase(name)) { return root; }
inti=0; Loggerlogger= root;
// check if the desired logger exists, if it does, return it // without further ado. LoggerchildLogger= (Logger) loggerCache.get(name); // if we have the child, then let us return it without wasting time if (childLogger != null) { return childLogger; }
// if the desired logger does not exist, them create all the loggers // in between as well (if they don't already exist) String childName; while (true) { inth= LoggerNameUtil.getSeparatorIndexOf(name, i); if (h == -1) { childName = name; } else { childName = name.substring(0, h); } // move i left of the last point i = h + 1; synchronized (logger) { childLogger = logger.getChildByName(childName); if (childLogger == null) { childLogger = logger.createChildByName(childName); loggerCache.put(childName, childLogger); incSize(); } } logger = childLogger; if (h == -1) { return childLogger; } } }