View Javadoc
1   package ch.qos.logback.classic.spi;
2   
3   import org.slf4j.ILoggerFactory;
4   import org.slf4j.IMarkerFactory;
5   import org.slf4j.helpers.BasicMarkerFactory;
6   import org.slf4j.helpers.Util;
7   import org.slf4j.spi.MDCAdapter;
8   import org.slf4j.spi.SLF4JServiceProvider;
9   
10  import ch.qos.logback.classic.LoggerContext;
11  import ch.qos.logback.classic.util.ContextInitializer;
12  import ch.qos.logback.classic.util.LogbackMDCAdapter;
13  import ch.qos.logback.core.CoreConstants;
14  import ch.qos.logback.core.joran.spi.JoranException;
15  import ch.qos.logback.core.status.StatusUtil;
16  import ch.qos.logback.core.util.StatusPrinter;
17  
18  public class LogbackServiceProvider implements SLF4JServiceProvider {
19  
20      final static String NULL_CS_URL = CoreConstants.CODES_URL + "#null_CS";
21  
22      /**
23       * Declare the version of the SLF4J API this implementation is compiled against.
24       * The value of this field is modified with each major release.
25       */
26      // to avoid constant folding by the compiler, this field must *not* be final
27      public static String REQUESTED_API_VERSION = "2.0.99"; // !final
28  
29      private LoggerContext defaultLoggerContext;
30      private IMarkerFactory markerFactory;
31      private LogbackMDCAdapter mdcAdapter;
32      // private final ContextSelectorStaticBinder contextSelectorBinder =
33      // ContextSelectorStaticBinder.getSingleton();
34  //    private static Object KEY = new Object();
35  //    private volatile boolean initialized = false;
36  
37      @Override
38      public void initialize() {
39          defaultLoggerContext = new LoggerContext();
40          defaultLoggerContext.setName(CoreConstants.DEFAULT_CONTEXT_NAME);
41          initializeLoggerContext();
42          defaultLoggerContext.start();
43          markerFactory = new BasicMarkerFactory();
44          mdcAdapter = new LogbackMDCAdapter();
45          // set the MDCAdapter for the defaultLoggerContext immediately
46          defaultLoggerContext.setMDCAdapter(mdcAdapter);
47      }
48  
49      private void initializeLoggerContext() {
50          try {
51              try {
52                  new ContextInitializer(defaultLoggerContext).autoConfig();
53              } catch (JoranException je) {
54                  Util.report("Failed to auto configure default logger context", je);
55              }
56              // LOGBACK-292
57              if (!StatusUtil.contextHasStatusListener(defaultLoggerContext)) {
58                  StatusPrinter.printInCaseOfErrorsOrWarnings(defaultLoggerContext);
59              }
60              // contextSelectorBinder.init(defaultLoggerContext, KEY);
61  
62          } catch (Exception t) { // see LOGBACK-1159
63              Util.report("Failed to instantiate [" + LoggerContext.class.getName() + "]", t);
64          }
65      }
66  
67      @Override
68  
69      public ILoggerFactory getLoggerFactory() {
70          return defaultLoggerContext;
71  
72  //        if (!initialized) {
73  //            return defaultLoggerContext;
74  //        
75  //
76  //        if (contextSelectorBinder.getContextSelector() == null) {
77  //            throw new IllegalStateException("contextSelector cannot be null. See also " + NULL_CS_URL);
78  //        }
79  //        return contextSelectorBinder.getContextSelector().getLoggerContext();
80      }
81  
82      @Override
83      public IMarkerFactory getMarkerFactory() {
84          return markerFactory;
85      }
86  
87      @Override
88      public MDCAdapter getMDCAdapter() {
89          return mdcAdapter;
90      }
91  
92      @Override
93      public String getRequestedApiVersion() {
94          return REQUESTED_API_VERSION;
95      }
96  
97  }