1 /**
2  * Logback: the reliable, generic, fast and flexible logging framework.
3  * Copyright (C) 1999-2015, 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 v1.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 package org.slf4j.impl;
15
16 import ch.qos.logback.core.status.StatusUtil;
17 import org.slf4j.ILoggerFactory;
18 import org.slf4j.LoggerFactory;
19 import org.slf4j.helpers.Util;
20 import org.slf4j.spi.LoggerFactoryBinder;
21
22 import ch.qos.logback.classic.LoggerContext;
23 import ch.qos.logback.classic.util.ContextInitializer;
24 import ch.qos.logback.classic.util.ContextSelectorStaticBinder;
25 import ch.qos.logback.core.CoreConstants;
26 import ch.qos.logback.core.joran.spi.JoranException;
27 import ch.qos.logback.core.util.StatusPrinter;
28
29 /**
30  * 
31  * The binding of {@link LoggerFactory} class with an actual instance of
32  * {@link ILoggerFactory} is performed using information returned by this class.
33  * 
34  * @author Ceki G&uuml;lc&uuml;</a>
35  */

36 public class StaticLoggerBinder implements LoggerFactoryBinder {
37
38     /**
39      * Declare the version of the SLF4J API this implementation is compiled
40      * against. The value of this field is usually modified with each release.
41      */

42     // to avoid constant folding by the compiler, this field must *not* be final
43     public static String REQUESTED_API_VERSION = "1.7.16"// !final
44
45     final static String NULL_CS_URL = CoreConstants.CODES_URL + "#null_CS";
46
47     /**
48      * The unique instance of this class.
49      */

50     private static StaticLoggerBinder SINGLETON = new StaticLoggerBinder();
51
52     private static Object KEY = new Object();
53
54     static {
55         SINGLETON.init();
56     }
57
58     private boolean initialized = false;
59     private LoggerContext defaultLoggerContext = new LoggerContext();
60     private final ContextSelectorStaticBinder contextSelectorBinder = ContextSelectorStaticBinder.getSingleton();
61
62     private StaticLoggerBinder() {
63         defaultLoggerContext.setName(CoreConstants.DEFAULT_CONTEXT_NAME);
64     }
65
66     public static StaticLoggerBinder getSingleton() {
67         return SINGLETON;
68     }
69
70     /**
71      * Package access for testing purposes.
72      */

73     static void reset() {
74         SINGLETON = new StaticLoggerBinder();
75         SINGLETON.init();
76     }
77
78     /**
79      * Package access for testing purposes.
80      */

81     void init() {
82         try {
83             try {
84                 new ContextInitializer(defaultLoggerContext).autoConfig();
85             } catch (JoranException je) {
86                 Util.report("Failed to auto configure default logger context", je);
87             }
88             // logback-292
89             if (!StatusUtil.contextHasStatusListener(defaultLoggerContext)) {
90                 StatusPrinter.printInCaseOfErrorsOrWarnings(defaultLoggerContext);
91             }
92             contextSelectorBinder.init(defaultLoggerContext, KEY);
93             initialized = true;
94         } catch (Exception t) { // see LOGBACK-1159
95             Util.report("Failed to instantiate [" + LoggerContext.class.getName() + "]", t);
96         }
97     }
98
99     public ILoggerFactory getLoggerFactory() {
100         if (!initialized) {
101             return defaultLoggerContext;
102         }
103
104         if (contextSelectorBinder.getContextSelector() == null) {
105             throw new IllegalStateException("contextSelector cannot be null. See also " + NULL_CS_URL);
106         }
107         return contextSelectorBinder.getContextSelector().getLoggerContext();
108     }
109
110     public String getLoggerFactoryClassStr() {
111         return contextSelectorBinder.getClass().getName();
112     }
113
114 }
115