1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.classic.spi;
16  
17  import org.slf4j.ILoggerFactory;
18  import org.slf4j.IMarkerFactory;
19  import org.slf4j.helpers.BasicMarkerFactory;
20  import org.slf4j.helpers.Util;
21  import org.slf4j.spi.MDCAdapter;
22  import org.slf4j.spi.SLF4JServiceProvider;
23  
24  import ch.qos.logback.classic.LoggerContext;
25  import ch.qos.logback.classic.util.ContextInitializer;
26  import ch.qos.logback.classic.util.LogbackMDCAdapter;
27  import ch.qos.logback.core.CoreConstants;
28  import ch.qos.logback.core.joran.spi.JoranException;
29  import ch.qos.logback.core.status.StatusUtil;
30  import ch.qos.logback.core.util.StatusPrinter;
31  
32  public class LogbackServiceProvider implements SLF4JServiceProvider {
33  
34      final static String NULL_CS_URL = CoreConstants.CODES_URL + "#null_CS";
35  
36      /**
37       * Declare the version of the SLF4J API this implementation is compiled against.
38       * The value of this field is modified with each major release.
39       */
40      // to avoid constant folding by the compiler, this field must *not* be final
41      public static String REQUESTED_API_VERSION = "2.0.99"; // !final
42  
43      private LoggerContext defaultLoggerContext = new LoggerContext();
44  
45  
46      // org.slf4j.LoggerFactory expects providers to initialize markerFactory as early as possible.
47      private IMarkerFactory markerFactory = new BasicMarkerFactory();
48  
49      // org.slf4j.LoggerFactory expects providers to initialize their MDCAdapter field
50      // as early as possible, preferably at construction time.
51      private LogbackMDCAdapter mdcAdapter = new LogbackMDCAdapter();
52  
53      @Override
54      public void initialize() {
55          defaultLoggerContext.setName(CoreConstants.DEFAULT_CONTEXT_NAME);
56          // set the MDCAdapter for the defaultLoggerContext immediately
57          defaultLoggerContext.setMDCAdapter(mdcAdapter);
58          initializeLoggerContext();
59          defaultLoggerContext.start();
60      }
61  
62      private void initializeLoggerContext() {
63          try {
64              try {
65                  new ContextInitializer(defaultLoggerContext).autoConfig();
66              } catch (JoranException je) {
67                  Util.report("Failed to auto configure default logger context", je);
68              }
69              // LOGBACK-292
70              if (!StatusUtil.contextHasStatusListener(defaultLoggerContext)) {
71                  StatusPrinter.printInCaseOfErrorsOrWarnings(defaultLoggerContext);
72              }
73              // contextSelectorBinder.init(defaultLoggerContext, KEY);
74  
75          } catch (Exception t) { // see LOGBACK-1159
76              Util.report("Failed to instantiate [" + LoggerContext.class.getName() + "]", t);
77          }
78      }
79  
80      @Override
81  
82      public ILoggerFactory getLoggerFactory() {
83          return defaultLoggerContext;
84      }
85  
86      @Override
87      public IMarkerFactory getMarkerFactory() {
88          return markerFactory;
89      }
90  
91      @Override
92      public MDCAdapter getMDCAdapter() {
93          return mdcAdapter;
94      }
95  
96      @Override
97      public String getRequestedApiVersion() {
98          return REQUESTED_API_VERSION;
99      }
100 
101 }