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.Reporter; 034import org.slf4j.helpers.Util; 035 036/** 037 * Log4jLoggerFactory is an implementation of {@link ILoggerFactory} returning 038 * the appropriate named {@link Reload4jLoggerAdapter} instance. 039 * 040 * @author Ceki Gülcü 041 */ 042public class Reload4jLoggerFactory implements ILoggerFactory { 043 044 private static final String LOG4J_DELEGATION_LOOP_URL = "http://www.slf4j.org/codes.html#log4jDelegationLoop"; 045 046 // check for delegation loops 047 static { 048 try { 049 Class.forName("org.apache.log4j.Log4jLoggerFactory"); 050 String part1 = "Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError. "; 051 String part2 = "See also " + LOG4J_DELEGATION_LOOP_URL + " for more details."; 052 053 Reporter.error(part1); 054 Reporter.error(part2); 055 throw new IllegalStateException(part1 + part2); 056 } catch (ClassNotFoundException e) { 057 // this is the good case 058 } 059 } 060 061 // key: name (String), value: a Log4jLoggerAdapter; 062 ConcurrentMap<String, Logger> loggerMap; 063 064 public Reload4jLoggerFactory() { 065 loggerMap = new ConcurrentHashMap<>(); 066 // force log4j to initialize 067 org.apache.log4j.LogManager.getRootLogger(); 068 } 069 070 /* 071 * (non-Javadoc) 072 * 073 * @see org.slf4j.ILoggerFactory#getLogger(java.lang.String) 074 */ 075 public Logger getLogger(String name) { 076 Logger slf4jLogger = loggerMap.get(name); 077 if (slf4jLogger != null) { 078 return slf4jLogger; 079 } else { 080 org.apache.log4j.Logger log4jLogger; 081 if (name.equalsIgnoreCase(Logger.ROOT_LOGGER_NAME)) 082 log4jLogger = LogManager.getRootLogger(); 083 else 084 log4jLogger = LogManager.getLogger(name); 085 086 Logger newInstance = new Reload4jLoggerAdapter(log4jLogger); 087 Logger oldInstance = loggerMap.putIfAbsent(name, newInstance); 088 return oldInstance == null ? newInstance : oldInstance; 089 } 090 } 091}