001/**
002 * Copyright (c) 2004-2011 QOS.ch
003 * All rights reserved.
004 *
005 * Permission is hereby granted, free  of charge, to any person obtaining
006 * a  copy  of this  software  and  associated  documentation files  (the
007 * "Software"), to  deal in  the Software without  restriction, including
008 * without limitation  the rights to  use, copy, modify,  merge, publish,
009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
010 * permit persons to whom the Software  is furnished to do so, subject to
011 * the following conditions:
012 *
013 * The  above  copyright  notice  and  this permission  notice  shall  be
014 * included in all copies or substantial portions of the Software.
015 *
016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023 *
024 */
025package org.slf4j.reload4j;
026
027import java.util.concurrent.ConcurrentHashMap;
028import java.util.concurrent.ConcurrentMap;
029
030import org.apache.log4j.LogManager;
031import org.slf4j.ILoggerFactory;
032import org.slf4j.Logger;
033import org.slf4j.helpers.Util;
034
035/**
036 * Log4jLoggerFactory is an implementation of {@link ILoggerFactory} returning
037 * the appropriate named {@link Reload4jLoggerAdapter} instance.
038 * 
039 * @author Ceki Gülcü
040 */
041public class Reload4jLoggerFactory implements ILoggerFactory {
042
043    private static final String LOG4J_DELEGATION_LOOP_URL = "http://www.slf4j.org/codes.html#log4jDelegationLoop";
044
045    // check for delegation loops
046    static {
047        try {
048            Class.forName("org.apache.log4j.Log4jLoggerFactory");
049            String part1 = "Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError. ";
050            String part2 = "See also " + LOG4J_DELEGATION_LOOP_URL + " for more details.";
051
052            Util.report(part1);
053            Util.report(part2);
054            throw new IllegalStateException(part1 + part2);
055        } catch (ClassNotFoundException e) {
056            // this is the good case
057        }
058    }
059
060    // key: name (String), value: a Log4jLoggerAdapter;
061    ConcurrentMap<String, Logger> loggerMap;
062
063    public Reload4jLoggerFactory() {
064        loggerMap = new ConcurrentHashMap<>();
065        // force log4j to initialize
066        org.apache.log4j.LogManager.getRootLogger();
067    }
068
069    /*
070     * (non-Javadoc)
071     * 
072     * @see org.slf4j.ILoggerFactory#getLogger(java.lang.String)
073     */
074    public Logger getLogger(String name) {
075        Logger slf4jLogger = loggerMap.get(name);
076        if (slf4jLogger != null) {
077            return slf4jLogger;
078        } else {
079            org.apache.log4j.Logger log4jLogger;
080            if (name.equalsIgnoreCase(Logger.ROOT_LOGGER_NAME))
081                log4jLogger = LogManager.getRootLogger();
082            else
083                log4jLogger = LogManager.getLogger(name);
084
085            Logger newInstance = new Reload4jLoggerAdapter(log4jLogger);
086            Logger oldInstance = loggerMap.putIfAbsent(name, newInstance);
087            return oldInstance == null ? newInstance : oldInstance;
088        }
089    }
090}