Log4jLoggerFactory xref

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.apache.log4j.spi.LoggerFactory;
    22  import org.slf4j.helpers.Util;
    23
    24  /**
    25   * This class is a factory that creates and maintains org.apache.log4j.Loggers
    26   * wrapping org.slf4j.Loggers.
    27   *
    28   * It keeps a hashtable of all created org.apache.log4j.Logger instances so that
    29   * all newly created instances are not dulpicates of existing loggers.
    30   *
    31   * @author S&eacute;bastien Pennec
    32   */
    33  class Log4jLoggerFactory {
    34
    35    // String, Logger
    36    private static Hashtable log4jLoggers = new Hashtable();
    37
    38    private static final String LOG4J_DELEGATION_LOOP_URL = "http://www.slf4j.org/codes.html#log4jDelegationLoop";
    39
    40    // check for delegation loops
    41    static {
    42      try {
    43        Class.forName("org.slf4j.impl.Log4jLoggerFactory");
    44        String part1 = "Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackOverflowError. ";
    45        String part2 = "See also " + LOG4J_DELEGATION_LOOP_URL
    46            + " 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 synchronized Logger getLogger(String name) {
    57      if (log4jLoggers.containsKey(name)) {
    58        return (org.apache.log4j.Logger) log4jLoggers.get(name);
    59      } else {
    60        Logger log4jLogger = new Logger(name);
    61
    62        log4jLoggers.put(name, log4jLogger);
    63        return log4jLogger;
    64      }
    65    }
    66
    67    public static synchronized Logger getLogger(String name, LoggerFactory loggerFactory) {
    68  	  if (log4jLoggers.containsKey(name)) {
    69  		  return (org.apache.log4j.Logger) log4jLoggers.get(name);
    70  	  } else {
    71  		  Logger log4jLogger = loggerFactory.makeNewLoggerInstance(name);
    72
    73  		  log4jLoggers.put(name, log4jLogger);
    74  		  return log4jLogger;
    75  	  }
    76    }
    77
    78  }