- 1 public class StaticLoggerBinder implements LoggerFactoryBinder {
- 2 3
- /**
- 4 * Declare the version of the SLF4J API this implementation is compiled
- 5 * against. The value of this field is usually modified with each release.
- 6 */
- 7 // to avoid constant folding by the compiler, this field must *not* be final
- 8 public static String REQUESTED_API_VERSION = "1.6"; // !final
- 9 10 final static String NULL_CS_URL = CoreConstants.CODES_URL + "#null_CS";
- 11 12
- /**
- 13 * The unique instance of this class.
- 14 */
- 15 private static StaticLoggerBinder SINGLETON = new StaticLoggerBinder();
- 16 17 private static Object KEY = new Object();
- 18 19 static {
- 20 SINGLETON.init();
- 21
- }
- 22 23 private boolean initialized = false;
- 24 private LoggerContext defaultLoggerContext = new LoggerContext();
- 25 private final ContextSelectorStaticBinder contextSelectorBinder = ContextSelectorStaticBinder 26.getSingleton();
- 27 28 private StaticLoggerBinder() {
- 29 defaultLoggerContext.setName(CoreConstants.DEFAULT_CONTEXT_NAME);
- 30
- }
- 31 32 public static StaticLoggerBinder getSingleton() {
- 33
- return SINGLETON;
- 34
- }
- 35 36
- /**
- 37 * Package access for testing purposes.
- 38 */
- 39 static void reset() {
- 40 SINGLETON = new StaticLoggerBinder();
- 41 SINGLETON.init();
- 42
- }
- 43 44
- /**
- 45 * Package access for testing purposes.
- 46 */
- 47 void init() {
- 48
- try {
- 49
- try {
- 50 new ContextInitializer(defaultLoggerContext).autoConfig();
- 51
- } catch(JoranException je) {
- 52 Util.report("Failed to auto configure default logger context", je);
- 53
- }
- 54 // logback-292
- 55
- if (!StatusUtil.contextHasStatusListener(defaultLoggerContext)) {
- 56 StatusPrinter.printInCaseOfErrorsOrWarnings(defaultLoggerContext);
- 57
- }
- 58 contextSelectorBinder.init(defaultLoggerContext, KEY);
- 59 initialized = true;
- 60
- } catch(Throwable t) {
- 61 // we should never get here
- 62 Util.report("Failed to instantiate [" + LoggerContext.class.getName() 63 + "]", t);
- 64
- }
- 65
- }
- 66 67 public ILoggerFactory getLoggerFactory() {
- 68
- if (!initialized) {
- 69
- return defaultLoggerContext;
- 70
- }
- 71 72
- if (contextSelectorBinder.getContextSelector() == null) {
- 73
- throw new IllegalStateException(74 "contextSelector cannot be null. See also " + NULL_CS_URL);
- 75
- }
- 76
- return contextSelectorBinder.getContextSelector().getLoggerContext();
- 77
- }
- 78 79 public String getLoggerFactoryClassStr() {
- 80
- return contextSelectorBinder.getClass().getName();
- 81
- }
- 82 83
- }
来源: