View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.log4j;
18  
19  import java.util.Hashtable;
20  
21  import org.slf4j.helpers.Util;
22  
23  /**
24   * This class is a factory that creates and maintains org.apache.log4j.Loggers
25   * wrapping org.slf4j.Loggers.
26   * 
27   * It keeps a hashtable of all created org.apache.log4j.Logger instances so that
28   * all newly created instances are not dulpicates of existing loggers.
29   * 
30   * @author Sébastien Pennec
31   */
32  class Log4jLoggerFactory {
33  
34    // String, Logger
35    private static Hashtable log4jLoggers = new Hashtable();
36  
37    private static final String LOG4J_DELEGATION_LOOP_URL = "http://www.slf4j.org/codes.html#log4jDelegationLoop";
38    
39    // check for delegation loops
40    static {
41      try {
42        Class.forName("org.slf4j.impl.Log4jLoggerFactory");
43        String part1 = "Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackOverflowError. ";
44        String part2 = "See also " + LOG4J_DELEGATION_LOOP_URL
45            + " for more details.";
46  
47        Util.report(part1);
48        Util.report(part2);
49              throw new IllegalStateException(part1 + part2);
50      } catch (ClassNotFoundException e) {
51        // this is the good case
52      }
53    }
54  
55    public static synchronized Logger getLogger(String name) {
56      if (log4jLoggers.containsKey(name)) {
57        return (org.apache.log4j.Logger) log4jLoggers.get(name);
58      } else {
59        Logger log4jLogger = new Logger(name);
60  
61        log4jLoggers.put(name, log4jLogger);
62        return log4jLogger;
63      }
64    }
65  
66  }