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 LogbackMDCAdapter 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 // set the MDCAdapter for the defaultLoggerContext immediately 046 defaultLoggerContext.setMDCAdapter(mdcAdapter); 047 } 048 049 private void initializeLoggerContext() { 050 try { 051 try { 052 new ContextInitializer(defaultLoggerContext).autoConfig(); 053 } catch (JoranException je) { 054 Util.report("Failed to auto configure default logger context", je); 055 } 056 // LOGBACK-292 057 if (!StatusUtil.contextHasStatusListener(defaultLoggerContext)) { 058 StatusPrinter.printInCaseOfErrorsOrWarnings(defaultLoggerContext); 059 } 060 // contextSelectorBinder.init(defaultLoggerContext, KEY); 061 062 } catch (Exception t) { // see LOGBACK-1159 063 Util.report("Failed to instantiate [" + LoggerContext.class.getName() + "]", t); 064 } 065 } 066 067 @Override 068 069 public ILoggerFactory getLoggerFactory() { 070 return defaultLoggerContext; 071 072// if (!initialized) { 073// return defaultLoggerContext; 074// 075// 076// if (contextSelectorBinder.getContextSelector() == null) { 077// throw new IllegalStateException("contextSelector cannot be null. See also " + NULL_CS_URL); 078// } 079// return contextSelectorBinder.getContextSelector().getLoggerContext(); 080 } 081 082 @Override 083 public IMarkerFactory getMarkerFactory() { 084 return markerFactory; 085 } 086 087 @Override 088 public MDCAdapter getMDCAdapter() { 089 return mdcAdapter; 090 } 091 092 @Override 093 public String getRequestedApiVersion() { 094 return REQUESTED_API_VERSION; 095 } 096 097}