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 + " for more details.";
47  
48              Util.report(part1);
49              Util.report(part2);
50              throw new IllegalStateException(part1 + part2);
51          } catch (ClassNotFoundException e) {
52              // this is the good case
53          }
54      }
55  
56      public static Logger getLogger(String name) {
57          org.apache.log4j.Logger instance = log4jLoggers.get(name);
58          if (instance != null) {
59              return instance;
60          } else {
61              Logger newInstance = new Logger(name);
62              Logger oldInstance = log4jLoggers.putIfAbsent(name, newInstance);
63              return oldInstance == null ? newInstance : oldInstance;
64          }
65      }
66  
67      public static Logger getLogger(String name, LoggerFactory loggerFactory) {
68          org.apache.log4j.Logger instance = log4jLoggers.get(name);
69          if (instance != null) {
70              return instance;
71          } else {
72              Logger newInstance = loggerFactory.makeNewLoggerInstance(name);
73              Logger oldInstance = log4jLoggers.putIfAbsent(name, newInstance);
74              return oldInstance == null ? newInstance : oldInstance;
75          }
76      }
77  }