001package ch.qos.logback.classic.spi; 002 003import org.slf4j.ILoggerFactory; 004import org.slf4j.IMarkerFactory; 005import org.slf4j.helpers.BasicMarkerFactory; 006import org.slf4j.helpers.Util; 007import org.slf4j.spi.MDCAdapter; 008import org.slf4j.spi.SLF4JServiceProvider; 009 010import ch.qos.logback.classic.LoggerContext; 011import ch.qos.logback.classic.util.ContextInitializer; 012import ch.qos.logback.classic.util.LogbackMDCAdapter; 013import ch.qos.logback.core.CoreConstants; 014import ch.qos.logback.core.joran.spi.JoranException; 015import ch.qos.logback.core.status.StatusUtil; 016import ch.qos.logback.core.util.StatusPrinter; 017 018public class LogbackServiceProvider implements SLF4JServiceProvider { 019 020 final static String NULL_CS_URL = CoreConstants.CODES_URL + "#null_CS"; 021 022 /** 023 * Declare the version of the SLF4J API this implementation is compiled against. 024 * The value of this field is modified with each major release. 025 */ 026 // to avoid constant folding by the compiler, this field must *not* be final 027 public static String REQUESTED_API_VERSION = "2.0.99"; // !final 028 029 private LoggerContext defaultLoggerContext; 030 private IMarkerFactory markerFactory; 031 private MDCAdapter mdcAdapter; 032 // private final ContextSelectorStaticBinder contextSelectorBinder = 033 // ContextSelectorStaticBinder.getSingleton(); 034// private static Object KEY = new Object(); 035// private volatile boolean initialized = false; 036 037 @Override 038 public void initialize() { 039 defaultLoggerContext = new LoggerContext(); 040 defaultLoggerContext.setName(CoreConstants.DEFAULT_CONTEXT_NAME); 041 initializeLoggerContext(); 042 defaultLoggerContext.start(); 043 markerFactory = new BasicMarkerFactory(); 044 mdcAdapter = new LogbackMDCAdapter(); 045 } 046 047 private void initializeLoggerContext() { 048 try { 049 try { 050 new ContextInitializer(defaultLoggerContext).autoConfig(); 051 } catch (JoranException je) { 052 Util.report("Failed to auto configure default logger context", je); 053 } 054 // LOGBACK-292 055 if (!StatusUtil.contextHasStatusListener(defaultLoggerContext)) { 056 StatusPrinter.printInCaseOfErrorsOrWarnings(defaultLoggerContext); 057 } 058 // contextSelectorBinder.init(defaultLoggerContext, KEY); 059 060 } catch (Exception t) { // see LOGBACK-1159 061 Util.report("Failed to instantiate [" + LoggerContext.class.getName() + "]", t); 062 } 063 } 064 065 @Override 066 067 public ILoggerFactory getLoggerFactory() { 068 return defaultLoggerContext; 069 070// if (!initialized) { 071// return defaultLoggerContext; 072// 073// 074// if (contextSelectorBinder.getContextSelector() == null) { 075// throw new IllegalStateException("contextSelector cannot be null. See also " + NULL_CS_URL); 076// } 077// return contextSelectorBinder.getContextSelector().getLoggerContext(); 078 } 079 080 @Override 081 public IMarkerFactory getMarkerFactory() { 082 return markerFactory; 083 } 084 085 @Override 086 public MDCAdapter getMDCAdapter() { 087 return mdcAdapter; 088 } 089 090 @Override 091 public String getRequestedApiVersion() { 092 return REQUESTED_API_VERSION; 093 } 094 095}