读logback源码系列文章(二)——提供ILoggerFactory
上篇博客介绍了logback是怎么对接slf4j的,简言之,就是通过下面这行代码
return StaticLoggerBinder.getSingleton().getLoggerFactory();
public interface LoggerFactoryBinder { /** * Return the instance of {@link ILoggerFactory} that * {@link org.slf4j.LoggerFactory} class should bind to. * * @return the instance of {@link ILoggerFactory} that * {@link org.slf4j.LoggerFactory} class should bind to. */ public ILoggerFactory getLoggerFactory(); /** * The String form of the {@link ILoggerFactory} object that this * <code>LoggerFactoryBinder</code> instance is <em>intended</em> to return. * * <p>This method allows the developer to intterogate this binder's intention * which may be different from the {@link ILoggerFactory} instance it is able to * yield in practice. The discrepency should only occur in case of errors. * * @return the class name of the intended {@link ILoggerFactory} instance */ public String getLoggerFactoryClassStr();}
private static StaticLoggerBinder SINGLETON = new StaticLoggerBinder();
public static StaticLoggerBinder getSingleton() { return SINGLETON; }
static { SINGLETON.init(); }
void init() { try { try { new ContextInitializer(defaultLoggerContext).autoConfig(); } catch (JoranException je) { Util.report("Failed to auto configure default logger context", je); } StatusPrinter.printInCaseOfErrorsOrWarnings(defaultLoggerContext); contextSelectorBinder.init(defaultLoggerContext, KEY); initialized = true; } catch (Throwable t) { // we should never get here Util.report("Failed to instantiate [" + LoggerContext.class.getName() + "]", t); } }
public void init(LoggerContext defaultLoggerContext, Object key) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { if(this.key == null) { this.key = key; } else if (this.key != key) { throw new IllegalAccessException("Only certain classes can access this method."); } String contextSelectorStr = OptionHelper .getSystemProperty(ClassicConstants.LOGBACK_CONTEXT_SELECTOR); if (contextSelectorStr == null) { contextSelector = new DefaultContextSelector(defaultLoggerContext); } else if (contextSelectorStr.equals("JNDI")) { // if jndi is specified, let's use the appropriate class contextSelector = new ContextJNDISelector(defaultLoggerContext); } else { contextSelector = dynamicalContextSelector(defaultLoggerContext, contextSelectorStr); } }
public ILoggerFactory getLoggerFactory() { if (!initialized) { return defaultLoggerContext; } if (contextSelectorBinder.getContextSelector() == null) { throw new IllegalStateException( "contextSelector cannot be null. See also " + NULL_CS_URL); } return contextSelectorBinder.getContextSelector().getLoggerContext(); }
public class DefaultContextSelector implements ContextSelector { private LoggerContext context; public DefaultContextSelector(LoggerContext context) { this.context = context; } public LoggerContext getLoggerContext() { return getDefaultLoggerContext(); } public LoggerContext getDefaultLoggerContext() { return context; }}