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 org.apache.log4j.spi.LoggerFactory;
20  import org.slf4j.helpers.Util;
21  
22  import java.util.concurrent.ConcurrentHashMap;
23  import java.util.concurrent.ConcurrentMap;
24  
25  /**
26   * This class is a factory that creates and maintains org.apache.log4j.Loggers
27   * wrapping org.slf4j.Loggers.
28   *
29   * It keeps a hashtable of all created org.apache.log4j.Logger instances so that
30   * all newly created instances are not duplicates of existing loggers.
31   *
32   * @author Sébastien Pennec
33   */
34  class Log4jLoggerFactory {
35  
36    // String, Logger
37    private static ConcurrentMap<String, Logger> log4jLoggers = new ConcurrentHashMap<String, Logger>();
38  
39    private static final String LOG4J_DELEGATION_LOOP_URL = "http://www.slf4j.org/codes.html#log4jDelegationLoop";
40  
41    // check for delegation loops
42    static {
43      try {
44        Class.forName("org.slf4j.impl.Log4jLoggerFactory");
45        String part1 = "Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackOverflowError. ";
46        String part2 = "See also " + LOG4J_DELEGATION_LOOP_URL
47                + " for more details.";
48  
49        Util.report(part1);
50        Util.report(part2);
51        throw new IllegalStateException(part1 + part2);
52      } catch (ClassNotFoundException e) {
53        // this is the good case
54      }
55    }
56  
57    public static Logger getLogger(String name) {
58      org.apache.log4j.Logger instance = log4jLoggers.get(name);
59      if (instance != null) {
60        return instance;
61      } else {
62        Logger newInstance = new Logger(name);
63        Logger oldInstance = log4jLoggers.putIfAbsent(name, newInstance);
64        return oldInstance == null ? newInstance : oldInstance;
65      }
66    }
67  
68    public static Logger getLogger(String name, LoggerFactory loggerFactory) {
69      org.apache.log4j.Logger instance = log4jLoggers.get(name);
70      if (instance != null) {
71        return instance;
72      } else {
73        Logger newInstance = loggerFactory.makeNewLoggerInstance(name);
74        Logger oldInstance = log4jLoggers.putIfAbsent(name, newInstance);
75        return oldInstance == null ? newInstance : oldInstance;
76      }
77    }
78  }