1 16 17 package org.apache.commons.logging.impl; 18 19 import java.util.Enumeration ; 20 import java.util.HashMap ; 21 import java.util.Hashtable ; 22 import java.util.Map ; 23 import java.util.Vector ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogConfigurationException; 27 import org.apache.commons.logging.LogFactory; 28 import org.slf4j.Logger; 29 import org.slf4j.LoggerFactory; 30 import org.slf4j.spi.LocationAwareLogger; 31 32 51 52 public class SLF4JLogFactory extends LogFactory { 53 54 56 60 Map loggerMap; 61 62 65 public SLF4JLogFactory() { 66 loggerMap = new HashMap (); 67 } 68 69 71 75 public static final String LOG_PROPERTY = "org.apache.commons.logging.Log"; 76 77 79 82 protected Hashtable attributes = new Hashtable (); 83 84 86 93 public Object getAttribute(String name) { 94 95 return (attributes.get(name)); 96 97 } 98 99 104 public String [] getAttributeNames() { 105 106 Vector names = new Vector (); 107 Enumeration keys = attributes.keys(); 108 while (keys.hasMoreElements()) { 109 names.addElement((String ) keys.nextElement()); 110 } 111 String results[] = new String [names.size()]; 112 for (int i = 0; i < results.length; i++) { 113 results[i] = (String ) names.elementAt(i); 114 } 115 return (results); 116 117 } 118 119 129 public Log getInstance(Class clazz) throws LogConfigurationException { 130 131 return (getInstance(clazz.getName())); 132 133 } 134 135 149 public Log getInstance(String name) throws LogConfigurationException { 150 Log instance = null; 151 synchronized (this) { 153 instance = (Log) loggerMap.get(name); 154 if (instance == null) { 155 Logger logger = LoggerFactory.getLogger(name); 156 if(logger instanceof LocationAwareLogger) { 157 instance = new SLF4JLocationAwareLog((LocationAwareLogger) logger); 158 } else { 159 instance = new SLF4JLog(logger); 160 } 161 loggerMap.put(name, instance); 162 } 163 } 164 return (instance); 165 166 } 167 168 175 public void release() { 176 System.out.println("WARN: The method " + SLF4JLogFactory.class 185 + "#release() was invoked."); 186 System.out 187 .println("WARN: Please see http://www.slf4j.org/codes.html for an explanation."); 188 System.out.flush(); 189 } 190 191 198 public void removeAttribute(String name) { 199 attributes.remove(name); 200 } 201 202 213 public void setAttribute(String name, Object value) { 214 215 if (value == null) { 216 attributes.remove(name); 217 } else { 218 attributes.put(name, value); 219 } 220 221 } 222 } | Popular Tags |