/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.log4j; import org.apache.log4j.spi.LoggerRepository; import org.apache.log4j.spi.LoggerFactory; import org.apache.log4j.spi.RepositorySelector; import org.apache.log4j.spi.DefaultRepositorySelector; import org.apache.log4j.spi.RootLogger; import org.apache.log4j.spi.NOPLoggerRepository; import org.apache.log4j.helpers.Loader; import org.apache.log4j.helpers.OptionConverter; import org.apache.log4j.helpers.LogLog; import java.net.URL; import java.net.MalformedURLException; import java.util.Enumeration; import java.io.StringWriter; import java.io.PrintWriter; /** * Gets {@link Logger} instances and operates on the current {@link LoggerRepository}. * *
* When the LogManager
class is loaded into memory the default initialization procedure runs. The default initialization
* procedure is described in the short log4j manual.
*
LoggerFactory
but only if the correct
guard is passed as parameter.
Initally the guard is null. If the guard is
null
, then invoking this method sets the logger
factory and the guard. Following invocations will throw a {@link
IllegalArgumentException}, unless the previously set
guard
is passed as the second parameter.
This allows a high-level component to set the {@link
RepositorySelector} used by the LogManager
.
For example, when tomcat starts it will be able to install its own repository selector. However, if and when Tomcat is embedded within JBoss, then JBoss will install its own repository selector and Tomcat will use the repository selector set by its container, JBoss. */ static public void setRepositorySelector(RepositorySelector selector, Object guard) throws IllegalArgumentException { if((LogManager.guard != null) && (LogManager.guard != guard)) { throw new IllegalArgumentException( "Attempted to reset the LoggerFactory without possessing the guard."); } if(selector == null) { throw new IllegalArgumentException("RepositorySelector must be non-null."); } LogManager.guard = guard; LogManager.repositorySelector = selector; } /** * This method tests if called from a method that * is known to result in class members being abnormally * set to null but is assumed to be harmless since the * all classes are in the process of being unloaded. * * @param ex exception used to determine calling stack. * @return true if calling stack is recognized as likely safe. */ private static boolean isLikelySafeScenario(final Exception ex) { StringWriter stringWriter = new StringWriter(); ex.printStackTrace(new PrintWriter(stringWriter)); String msg = stringWriter.toString(); return msg.indexOf("org.apache.catalina.loader.WebappClassLoader.stop") != -1; } static public LoggerRepository getLoggerRepository() { if (repositorySelector == null) { repositorySelector = new DefaultRepositorySelector(new NOPLoggerRepository()); guard = null; Exception ex = new IllegalStateException("Class invariant violation"); String msg = "log4j called after unloading, see http://logging.apache.org/log4j/1.2/faq.html#unload."; if (isLikelySafeScenario(ex)) { LogLog.debug(msg, ex); } else { LogLog.error(msg, ex); } } return repositorySelector.getLoggerRepository(); } /** Retrieve the appropriate root logger. */ public static Logger getRootLogger() { // Delegate the actual manufacturing of the logger to the logger repository. return getLoggerRepository().getRootLogger(); } /** Retrieve the appropriate {@link Logger} instance. */ public static Logger getLogger(final String name) { // Delegate the actual manufacturing of the logger to the logger repository. return getLoggerRepository().getLogger(name); } /** Retrieve the appropriate {@link Logger} instance. */ public static Logger getLogger(final Class clazz) { // Delegate the actual manufacturing of the logger to the logger repository. return getLoggerRepository().getLogger(clazz.getName()); } /** Retrieve the appropriate {@link Logger} instance. */ public static Logger getLogger(final String name, final LoggerFactory factory) { // Delegate the actual manufacturing of the logger to the logger repository. return getLoggerRepository().getLogger(name, factory); } public static Logger exists(final String name) { return getLoggerRepository().exists(name); } public static Enumeration getCurrentLoggers() { return getLoggerRepository().getCurrentLoggers(); } public static void shutdown() { getLoggerRepository().shutdown(); } public static void resetConfiguration() { getLoggerRepository().resetConfiguration(); } }