Modify the log4j starting mode from the source code

Source: Internet
Author: User
Tags deprecated manual throwable jboss tomcat java throws log4j

The startup of Log4j is controlled by the Logmanager class, and the static block of code in Logmanager determines that it will be loaded when the system is started.

But sometimes in order for it to delay loading, the related variables are passed to the log4j by the spring container, which needs to be loaded after the spring container is started.

Modify the static code block to delete the self-starting part:

static {//By default we use a defaultrepositoryselector which always returns ' H '.
    Hierarchy h = new Hierarchy (new Rootlogger (Level.debug));

    Repositoryselector = new Defaultrepositoryselector (h);  /** Search for the properties file Log4j.properties in the CLASSPATH.

    */String override =optionconverter.getsystemproperty (Default_init_override_key, NULL);
    If there is no default init override, then get the resource//specified by the user or the default Config file. if (override = = NULL | | "false". Equalsignorecase (Override)) {String configurationoptionstr = Optionconverter.getsystemproperty (D

      Efault_configuration_key, NULL); String configuratorclassname = Optionconverter.getsystemproperty (confi

      Gurator_class_key, NULL);

      URL url = null; If the user has not specified the Log4j.configuration//property, we search first for thE file "Log4j.xml" and then/"Log4j.properties" if (configurationoptionstr = = null) {URL = Loader.getresou
	Rce (Default_xml_configuration_file);
	if (url = = null) {URL = Loader.getresource (default_configuration_file);
	}} else {try {url = new URL (configurationoptionstr); } catch (Malformedurlexception ex) {//So, resource are not a URL://Attempt to get the resource from the class pat 
	H url = loader.getresource (CONFIGURATIONOPTIONSTR); }}//If We have a non-null URL and then delegate the rest of the//configuration to the Optionco
      Nverter.selectandconfigure//method.
        if (URL! = null) {loglog.debug ("Using URL [" +url+ "] for automatic log4j configuration."); try {optionconverter.selectandconfigure (URL, Configuratorclassname, logmanager.getloggerrepository ())
        ;
        } catch (Noclassdeffounderror e) {Loglog.warn ("Error during default initialization", E); }
      } else {loglog.debug ("Could not find resource: [" +configurationoptionstr+ "]."); }} else {Loglog.debug ("Default initialization of overridden by" + Default_init_override_key + 
    "Property.");  }  
  }

After modification:

static{
	  	Hierarchy h = new Hierarchy (new Rootlogger (level) level.debug);
	    Repositoryselector = new Defaultrepositoryselector (h);
  }

Org.apache.log4j.LogManager

/* * 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 are not a use of this file except in compliance with * the License. Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by a Pplicable law or agreed to in writing, software * Distributed under the License are distributed on a "as is" BASIS, * WI
 Thout 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 Java.io.PrintWriter;
Import Java.io.StringWriter;
Import java.net.MalformedURLException;
Import Java.net.URL;

Import java.util.Enumeration; Import ORG.APACHE.LOg4j.helpers.Loader;
Import Org.apache.log4j.helpers.LogLog;
Import Org.apache.log4j.helpers.OptionConverter;
Import Org.apache.log4j.spi.DefaultRepositorySelector;
Import Org.apache.log4j.spi.LoggerFactory;
Import Org.apache.log4j.spi.LoggerRepository;
Import Org.apache.log4j.spi.NOPLoggerRepository;
Import Org.apache.log4j.spi.RepositorySelector;

Import Org.apache.log4j.spi.RootLogger; /** * Use the <code>LogManager</code> class to retreive {@link Logger} * instances or to operate on the Curr ent {@link * loggerrepository}. When the <code>LogManager</code> class is loaded * to memory the default initalzation procedure is Inititat Ed. The * default Intialization procedure</a> is described in the <a * href= ". /.. /.. /..
 /manual.html#defaultinit ">short log4j manual</a>. * * @author ceki gülcü*/public class Logmanager {/** * @deprecated this variable are for internal use only. It'll * become package protected on future versionS. * */static public final String default_configuration_file = "Log4j.properties";  
   
  Static final String default_xml_configuration_file = "Log4j.xml"; /** * @deprecated This variable are for internal use only.
   It'll * become private in the future versions.

  * */static final public String default_configuration_key= "Log4j.configuration"; /** * @deprecated This variable are for internal use only.
   It'll * become private in the future versions.

  * */static final public String configurator_class_key= "Log4j.configuratorclass"; /** * @deprecated This variable are for internal use only.
  It'll * become private in the future versions. */public static final String Default_init_override_key = "Log4j.defau


  Ltinitoverride ";
  Static private Object guard = null;

  static private Repositoryselector repositoryselector;
	    static{Hierarchy h = new Hierarchy (new Rootlogger (level) level.debug); ReposItoryselector = new Defaultrepositoryselector (h); }/** sets <code>LoggerFactory</code> but only if the correct <em>guard</em> is PA
     
     ssed as parameter.  <p>initally the guard is null. If the Guard is <code>null</code>, then invoking this method sets the logger factory and the guard. Following invocations would throw a {@link illegalargumentexception}, unless the previously set <code>guard

     </code> is passed as the second parameter. <p>this allows a high-level component to set the {@link repositoryselector} used by the <code>logmanager&
     
     Lt;/code>. <p>for example, when Tomcat starts it would be able to the install its own repository selector. However, if and when Tomcat was embedded within JBoss, then JBoss would install its own repository selector and to  MCAT would use the repository selector set to 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 which is known to the result in class members being Abnorma
     Lly * Set to NULL if assumed to being harmless since the * All classes was 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) {Repositoryse
        Lector = 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 T
    He 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 T
    He 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 mans
    Ufacturing 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 ();
 }
}

By reading Source: The org.apache.log4j.helpers.optionconverter,log4j variable is obtained from the system variable.

/**
     Very similar to <code>System.getProperty</code> except, the
     {@link SecurityException} is Hidden.

     @param key The key to search for.
     @param def The default value to return.
     @return The string value of the system property, or the default
     value if there are no property with that key.

     @since 1.1 *
  /public static
  string Getsystemproperty (String key, String def) {
    try {
      return System.getproperty (key, Def);
    } catch (Throwable e) {//Ms-java throws Com.ms.security.SecurityExceptionEx
      Loglog.debug ("is not allowed to read Sys Tem property \ "+key+" \ ".");
      return def;
    }
  }

You can modify this method to remove variables from other places, such as from an environment variable:

public static string Getenvproperty (String key, String def) {
    try {
        string value = System.getenv (key);
        if (value = = null)
        {
            value = def;
        }
      return value;
    } catch (Throwable e) {//Ms-java throws Com.ms.security.SecurityExceptionEx

      Loglog.debug ("is not allowed to read Syst Em property \ "+key+" \ ".");
      return def;
    }
  }


Add the method to the Getsystemproperties () method

First try in System properties
     String replacement = Getsystemproperty (key, null);

     Then try in Environment Properties
     if (replacement = = null)
     {
         replacement = Getenvproperty (key, NULL); c17/>}

     //Then try props parameter
     if (replacement = = NULL && props! = null) {
     replacement = PROPS.G Etproperty (key);
     }

Therefore, the variables that need to be changed are passed to the system variable.

Import Java.util.Map;

Import Javax.servlet.ServletContext;
Import Org.apache.log4j.xml.DOMConfigurator;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import Org.springframework.beans.factory.InitializingBean;

Import Org.springframework.web.context.ServletContextAware; public class Log4jconfigbean implements Initializingbean, Servletcontextaware {private static Logger Logger = Loggerfa
	
	Ctory.getlogger (Log4jconfigbean.class);

	Private Springpropertyconfigurer Propertyconfigurer; @Override public void Setservletcontext (ServletContext servletcontext) {@SuppressWarnings ("static-access") Ma
		p<string, object> mvalue = Propertyconfigurer.getctxpropertiesmap ();  For (String Key:mValue.keySet ()) {Logger.debug ("* * * Config application param: {" + key + ":" + mvalue.get (key) +
			" }*****" );
			if (Key.startswith ("log4j")) {System.setproperty (key, Mvalue.get (key). ToString ()); }} String Filepath=servletcontext.getrealpath ("/");
	Domconfigurator.configure (FilePath + "/web-inf/classes/log4j.xml"); } @Override public void Afterpropertiesset () throws Exception {} public void Setpropertyconfigurer (Springpro
	Pertyconfigurer propertyconfigurer) {this.propertyconfigurer = Propertyconfigurer; }

}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.