View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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 org.slf4j.ILoggerFactory;
17  import org.slf4j.LoggerFactory;
18  import org.slf4j.helpers.Util;
19  import org.slf4j.spi.LoggerFactoryBinder;
20  
21  import ch.qos.logback.classic.LoggerContext;
22  import ch.qos.logback.classic.util.ContextInitializer;
23  import ch.qos.logback.classic.util.ContextSelectorStaticBinder;
24  import ch.qos.logback.core.CoreConstants;
25  import ch.qos.logback.core.joran.spi.JoranException;
26  import ch.qos.logback.core.util.StatusPrinter;
27  
28  /**
29   * 
30   * The binding of {@link LoggerFactory} class with an actual instance of
31   * {@link ILoggerFactory} is performed using information returned by this class.
32   * 
33   * @author Ceki G&uuml;lc&uuml;</a>
34   */
35  public class StaticLoggerBinder implements LoggerFactoryBinder {
36  
37    /**
38     * Declare the version of the SLF4J API this implementation is compiled
39     * against. The value of this field is usually modified with each release.
40     */
41    // to avoid constant folding by the compiler, this field must *not* be final
42    public static String REQUESTED_API_VERSION = "1.6"; // !final
43  
44    final static String NULL_CS_URL = CoreConstants.CODES_URL + "#null_CS";
45  
46    /**
47     * The unique instance of this class.
48     */
49    private static StaticLoggerBinder SINGLETON = new StaticLoggerBinder();
50  
51    private static Object KEY = new Object();
52  
53    static {
54      SINGLETON.init();
55    }
56  
57    private boolean initialized = false;
58    private LoggerContext defaultLoggerContext = new LoggerContext();
59    private final ContextSelectorStaticBinder contextSelectorBinder = ContextSelectorStaticBinder
60        .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        StatusPrinter.printInCaseOfErrorsOrWarnings(defaultLoggerContext);
89        contextSelectorBinder.init(defaultLoggerContext, KEY);
90        initialized = true;
91      } catch (Throwable t) {
92        // we should never get here
93        Util.report("Failed to instantiate [" + LoggerContext.class.getName()
94            + "]", t);
95      }
96    }
97  
98    public ILoggerFactory getLoggerFactory() {
99      if (!initialized) {
100       return defaultLoggerContext;
101     }
102 
103     if (contextSelectorBinder.getContextSelector() == null) {
104       throw new IllegalStateException(
105           "contextSelector cannot be null. See also " + NULL_CS_URL);
106     }
107     return contextSelectorBinder.getContextSelector().getLoggerContext();
108   }
109 
110   public String getLoggerFactoryClassStr() {
111     return contextSelectorBinder.getContextSelector().getClass().getName();
112   }
113 
114 }