1 16 17 package org.apache.log4j; 18 19 import org.apache.log4j.spi.LoggerRepository; 20 import org.apache.log4j.spi.LoggerFactory; 21 import org.apache.log4j.spi.RepositorySelector; 22 import org.apache.log4j.spi.DefaultRepositorySelector; 23 import org.apache.log4j.spi.RootLogger; 24 import org.apache.log4j.helpers.Loader; 25 import org.apache.log4j.helpers.OptionConverter; 26 import org.apache.log4j.helpers.LogLog; 27 28 import java.net.URL ; 29 import java.net.MalformedURLException ; 30 31 32 import java.util.Enumeration ; 33 34 43 public class LogManager { 44 45 49 static public final String DEFAULT_CONFIGURATION_FILE = "log4j.properties"; 50 51 static final String DEFAULT_XML_CONFIGURATION_FILE = "log4j.xml"; 52 53 57 static final public String DEFAULT_CONFIGURATION_KEY="log4j.configuration"; 58 59 63 static final public String CONFIGURATOR_CLASS_KEY="log4j.configuratorClass"; 64 65 69 public static final String DEFAULT_INIT_OVERRIDE_KEY = 70 "log4j.defaultInitOverride"; 71 72 73 static private Object guard = null; 74 static private RepositorySelector repositorySelector; 75 76 static { 77 Hierarchy h = new Hierarchy(new RootLogger((Level) Level.DEBUG)); 79 repositorySelector = new DefaultRepositorySelector(h); 80 81 82 String override =OptionConverter.getSystemProperty(DEFAULT_INIT_OVERRIDE_KEY, 83 null); 84 85 if(override == null || "false".equalsIgnoreCase(override)) { 88 89 String configurationOptionStr = OptionConverter.getSystemProperty( 90 DEFAULT_CONFIGURATION_KEY, 91 null); 92 93 String configuratorClassName = OptionConverter.getSystemProperty( 94 CONFIGURATOR_CLASS_KEY, 95 null); 96 97 URL url = null; 98 99 if(configurationOptionStr == null) { 103 url = Loader.getResource(DEFAULT_XML_CONFIGURATION_FILE); 104 if(url == null) { 105 url = Loader.getResource(DEFAULT_CONFIGURATION_FILE); 106 } 107 } else { 108 try { 109 url = new URL (configurationOptionStr); 110 } catch (MalformedURLException ex) { 111 url = Loader.getResource(configurationOptionStr); 114 } 115 } 116 117 if(url != null) { 121 LogLog.debug("Using URL ["+url+"] for automatic log4j configuration."); 122 OptionConverter.selectAndConfigure(url, configuratorClassName, 123 LogManager.getLoggerRepository()); 124 } else { 125 LogLog.debug("Could not find resource: ["+configurationOptionStr+"]."); 126 } 127 } 128 } 129 130 148 static 149 public 150 void setRepositorySelector(RepositorySelector selector, Object guard) 151 throws IllegalArgumentException { 152 if((LogManager.guard != null) && (LogManager.guard != guard)) { 153 throw new IllegalArgumentException ( 154 "Attempted to reset the LoggerFactory without possessing the guard."); 155 } 156 157 if(selector == null) { 158 throw new IllegalArgumentException ("RepositorySelector must be non-null."); 159 } 160 161 LogManager.guard = guard; 162 LogManager.repositorySelector = selector; 163 } 164 165 static 166 public 167 LoggerRepository getLoggerRepository() { 168 return repositorySelector.getLoggerRepository(); 169 } 170 171 174 public 175 static 176 Logger getRootLogger() { 177 return repositorySelector.getLoggerRepository().getRootLogger(); 179 } 180 181 184 public 185 static 186 Logger getLogger(String name) { 187 return repositorySelector.getLoggerRepository().getLogger(name); 189 } 190 191 194 public 195 static 196 Logger getLogger(Class clazz) { 197 return repositorySelector.getLoggerRepository().getLogger(clazz.getName()); 199 } 200 201 202 205 public 206 static 207 Logger getLogger(String name, LoggerFactory factory) { 208 return repositorySelector.getLoggerRepository().getLogger(name, factory); 210 } 211 212 public 213 static 214 Logger exists(String name) { 215 return repositorySelector.getLoggerRepository().exists(name); 216 } 217 218 public 219 static 220 Enumeration getCurrentLoggers() { 221 return repositorySelector.getLoggerRepository().getCurrentLoggers(); 222 } 223 224 public 225 static 226 void shutdown() { 227 repositorySelector.getLoggerRepository().shutdown(); 228 } 229 230 public 231 static 232 void resetConfiguration() { 233 repositorySelector.getLoggerRepository().resetConfiguration(); 234 } 235 } 236 237 | Popular Tags |