1   /* 
2    * Copyright (c) 2004-2007 QOS.ch
3    * All rights reserved.
4    * 
5    * Permission is hereby granted, free  of charge, to any person obtaining
6    * a  copy  of this  software  and  associated  documentation files  (the
7    * "Software"), to  deal in  the Software without  restriction, including
8    * without limitation  the rights to  use, copy, modify,  merge, publish,
9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
10   * permit persons to whom the Software  is furnished to do so, subject to
11   * the following conditions:
12   * 
13   * The  above  copyright  notice  and  this permission  notice  shall  be
14   * included in all copies or substantial portions of the Software.
15   * 
16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23   */
24  
25  package org.slf4j;
26  
27  import org.slf4j.helpers.Util;
28  import org.slf4j.impl.StaticLoggerBinder;
29   
30  /**
31   * The <code>LoggerFactory</code> is a utility class producing Loggers for
32   * various logging APIs, most notably for log4j, logback and JDK 1.4 logging. 
33   * Other implementations such as {@link org.slf4j.impl.NOPLogger NOPLogger} and
34   * {@link org.slf4j.impl.SimpleLogger SimpleLogger} are also supported.
35   * 
36   * <p>
37   * <code>LoggerFactory</code> is essentially a wrapper around an
38   * {@link ILoggerFactory} instance bound with <code>LoggerFactory</code> at
39   * compile time.
40   * 
41   * <p>
42   * Please note that all methods in <code>LoggerFactory</code> are static.
43   * 
44   * @author Ceki G&uuml;lc&uuml;
45   */
46  public final class LoggerFactory {
47  
48    static ILoggerFactory loggerFactory;
49  
50    static final String NO_STATICLOGGERBINDER_URL = "http://www.slf4j.org/codes.html#StaticLoggerBinder";
51    static final String NULL_LF_URL = "http://www.slf4j.org/codes.html#null_LF";
52      
53    // private constructor prevents instantiation
54    private LoggerFactory() {
55    }
56  
57  
58    static {
59      try { 
60        loggerFactory = StaticLoggerBinder.SINGLETON.getLoggerFactory();
61      } catch(NoClassDefFoundError ncde) {
62        String msg = ncde.getMessage();
63        if(msg != null && msg.indexOf("org/slf4j/impl/StaticLoggerBinder") != -1) {
64          Util.reportFailure("Failed to load class \"org.slf4j.impl.StaticLoggerBinder\".");
65          Util.reportFailure("See "+NO_STATICLOGGERBINDER_URL+" for further details.");
66          
67        } 
68        throw ncde;
69      } catch (Exception e) {
70        // we should never get here
71        Util.reportFailure("Failed to instantiate logger ["
72            + StaticLoggerBinder.SINGLETON.getLoggerFactoryClassStr() + "]", e);
73      }
74    }
75  
76    /**
77     * Return a logger named according to the name parameter using the statically
78     * bound {@link ILoggerFactory} instance.
79     * 
80     * @param name
81     *          The name of the logger.
82     * @return logger
83     */
84    public static Logger getLogger(String name) {
85      if(loggerFactory == null) {
86        throw new IllegalStateException("Logging factory implementation cannot be null. See also "+NULL_LF_URL);
87      }
88      return loggerFactory.getLogger(name);
89    }
90  
91    /**
92     * Return a logger named corresponding to the class passed as parameter, using
93     * the statically bound {@link ILoggerFactory} instance.
94     * 
95     * @param clazz
96     *          the returned logger will be named after clazz
97     * @return logger
98     */
99    public static Logger getLogger(Class clazz) {
100     if(loggerFactory == null) {
101       throw new IllegalStateException("Logging factory implementation cannot be null. See also "+NULL_LF_URL);
102     }
103     return loggerFactory.getLogger(clazz.getName());
104   }
105 
106   /**
107    * Return the {@link ILoggerFactory} instance in use.
108    * 
109    * <p>ILoggerFactory instance is bound with this class at compile
110    * time.
111    * 
112    * @return the ILoggerFactory instance in use
113    */
114   public static ILoggerFactory getILoggerFactory() {
115     return loggerFactory;
116   }
117 }