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.impl; 026 027import java.util.concurrent.ConcurrentHashMap; 028import java.util.concurrent.ConcurrentMap; 029 030import org.apache.commons.logging.LogFactory; 031import org.slf4j.ILoggerFactory; 032import org.slf4j.Logger; 033import org.slf4j.helpers.Util; 034 035/** 036 * JCLLoggerFactory is an implementation of {@link ILoggerFactory} returning the 037 * appropriately named {@link JCLLoggerAdapter} instance. 038 * 039 * @author Ceki Gülcü 040 */ 041public class JCLLoggerFactory implements ILoggerFactory { 042 043 private static final String JCL_DELEGATION_LOOP_URL = "http://www.slf4j.org/codes.html#jclDelegationLoop"; 044 045 // check for delegation loops 046 static { 047 try { 048 Class.forName("org.apache.commons.logging.impl.SLF4JLogFactory"); 049 String part1 = "Detected both jcl-over-slf4j.jar AND bound slf4j-jcl.jar on the class path, preempting StackOverflowError. "; 050 String part2 = "See also " + JCL_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 JCLLoggerAdapter; 061 final ConcurrentMap<String, Logger> loggerMap; 062 063 public JCLLoggerFactory() { 064 loggerMap = new ConcurrentHashMap<String, Logger>(); 065 } 066 067 /* 068 * (non-Javadoc) 069 * 070 * @see org.slf4j.ILoggerFactory#getLogger(java.lang.String) 071 */ 072 public Logger getLogger(String name) { 073 Logger slf4jLogger = loggerMap.get(name); 074 if (slf4jLogger != null) { 075 return slf4jLogger; 076 } else { 077 org.apache.commons.logging.Log jclLogger = LogFactory.getLog(name); 078 Logger newInstance = new JCLLoggerAdapter(jclLogger, name); 079 Logger oldInstance = loggerMap.putIfAbsent(name, newInstance); 080 return oldInstance == null ? newInstance : oldInstance; 081 } 082 } 083}