1 /* 2 * Copyright (c) 2004-2005 SLF4J.ORG 3 * Copyright (c) 2004-2005 QOS.ch 4 * 5 * All rights reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining 8 * a copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, and/or sell copies of the Software, and to permit persons 12 * to whom the Software is furnished to do so, provided that the above 13 * copyright notice(s) and this permission notice appear in all copies of 14 * the Software and that both the above copyright notice(s) and this 15 * permission notice appear in supporting documentation. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 20 * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 21 * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY 22 * SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER 23 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF 24 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 25 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 26 * 27 * Except as contained in this notice, the name of a copyright holder 28 * shall not be used in advertising or otherwise to promote the sale, use 29 * or other dealings in this Software without prior written authorization 30 * of the copyright holder. 31 * 32 */ 33 34 package org.slf4j.impl; 35 36 import java.util.HashMap; 37 import java.util.Map; 38 39 import org.apache.commons.logging.LogFactory; 40 import org.slf4j.ILoggerFactory; 41 import org.slf4j.Logger; 42 import org.slf4j.helpers.Util; 43 44 /** 45 * JCLLoggerFactory is an implementation of {@link ILoggerFactory} returning the 46 * appropriately named {@link JCLLoggerAdapter} instance. 47 * 48 * @author Ceki Gülcü 49 */ 50 public class JCLLoggerFactory implements ILoggerFactory { 51 52 private static final String JCL_DELEGATION_LOOP_URL = "http://www.slf4j.org/codes.html#jclDelegationLoop"; 53 54 // check for delegation loops 55 static { 56 try { 57 Class.forName("org.apache.commons.logging.impl.SLF4JLogFactory"); 58 String part1 = "Detected both jcl-over-slf4j.jar AND slf4j-jcl.jar on the class path, preempting StackOverflowError. "; 59 String part2 = "See also " + JCL_DELEGATION_LOOP_URL 60 + " for more details."; 61 62 Util.report(part1); 63 Util.report(part2); 64 throw new IllegalStateException(part1 + part2); 65 } catch (ClassNotFoundException e) { 66 // this is the good case 67 } 68 } 69 70 // key: name (String), value: a JCLLoggerAdapter; 71 Map loggerMap; 72 73 public JCLLoggerFactory() { 74 loggerMap = new HashMap(); 75 } 76 77 /* 78 * (non-Javadoc) 79 * 80 * @see org.slf4j.ILoggerFactory#getLogger(java.lang.String) 81 */ 82 public Logger getLogger(String name) { 83 Logger logger = null; 84 // protect against concurrent access of loggerMap 85 synchronized (this) { 86 logger = (Logger) loggerMap.get(name); 87 if (logger == null) { 88 org.apache.commons.logging.Log jclLogger = LogFactory.getLog(name); 89 logger = new JCLLoggerAdapter(jclLogger, name); 90 loggerMap.put(name, logger); 91 } 92 } 93 return logger; 94 } 95 }