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 = "1.8.99"; // !final
28  
29      private LoggerContext defaultLoggerContext;
30      private IMarkerFactory markerFactory;
31      private MDCAdapter mdcAdapter;
32      // private final ContextSelectorStaticBinder contextSelectorBinder = ContextSelectorStaticBinder.getSingleton();
33  //    private static Object KEY = new Object();
34  //    private volatile boolean initialized = false;
35      
36      @Override
37      public void initialize() {
38          defaultLoggerContext = new LoggerContext();
39          defaultLoggerContext.setName(CoreConstants.DEFAULT_CONTEXT_NAME);
40          initializeLoggerContext();
41          markerFactory = new BasicMarkerFactory();
42          mdcAdapter = new LogbackMDCAdapter();
43          //initialized = true;
44      }
45  
46      private void initializeLoggerContext() {
47          try {
48              try {
49                  new ContextInitializer(defaultLoggerContext).autoConfig();
50              } catch (JoranException je) {
51                  Util.report("Failed to auto configure default logger context", je);
52              }
53              // logback-292
54              if (!StatusUtil.contextHasStatusListener(defaultLoggerContext)) {
55                  StatusPrinter.printInCaseOfErrorsOrWarnings(defaultLoggerContext);
56              }
57              //contextSelectorBinder.init(defaultLoggerContext, KEY);
58             
59          } catch (Exception t) { // see LOGBACK-1159
60              Util.report("Failed to instantiate [" + LoggerContext.class.getName() + "]", t);
61          }
62      }
63  
64      @Override
65  
66      public ILoggerFactory getLoggerFactory() {
67          return defaultLoggerContext;
68          
69  //        if (!initialized) {
70  //            return defaultLoggerContext;
71  //        
72  //
73  //        if (contextSelectorBinder.getContextSelector() == null) {
74  //            throw new IllegalStateException("contextSelector cannot be null. See also " + NULL_CS_URL);
75  //        }
76  //        return contextSelectorBinder.getContextSelector().getLoggerContext();
77      }
78  
79      @Override
80      public IMarkerFactory getMarkerFactory() {
81          return markerFactory;
82      }
83  
84      @Override
85      public MDCAdapter getMDCAdapter() {
86          return mdcAdapter;
87      }
88  
89      @Override
90      public String getRequesteApiVersion() {
91          return REQUESTED_API_VERSION;
92      }
93  
94  }