Initialize the Logback variable

Source: Internet
Author: User
Tags tostring tomcat apache tomcat log4j

For ease of configuration, we put the parameters of the Logback variables into the configuration file system.properties. When the Tomcat container starts, it controls the loading order of the container, loads the properties file, and initializes the Logback. Since Logback is initialized by a static block of code in the Staticloggerbinder, it is necessary to study the order in which the tomncat is initialized before it is loaded into the configuration file.

Now, let's see what the Tomcat launcher did.

October 13, 2016 8:58:29 am org.apache.tomcat.util.digester.SetPropertiesRule begin warning: [setpropertiesrule]{server/service/ Engine/host/context} Setting Property ' source ' to ' Org.eclipse.jst.j2ee.server:ecf.meloans ' do not find a matching prope
Rty. October 13, 2016 8:58:29 am org.apache.catalina.startup.VersionLoggerListener log information: Server version:apache tomcat/7.0. 680 m 13, 2016 8:58:29 am org.apache.catalina.startup.VersionLoggerListener log information: Server Built:feb 8 2016 20:2
5:54 UTC October 13, 2016 8:58:29 am org.apache.catalina.startup.VersionLoggerListener log info: Server number:7.0.68.0 October 13, 2016 8:58:29 am org.apache.catalina.startup.VersionLoggerListener log info: OS name:windows 70 Month 13, 2  016 8:58:29 am Org.apache.catalina.startup.VersionLoggerListener Log info: OS version:6.1 October 13, 2016 8:58:29 am Org.apache.catalina.startup.VersionLoggerListener Log Info: architecture:amd64 October 13, 2016 8:58:29 am Org.apache. Catalina.startup.VersionLoggerlistener Log Info: Java home:d:\program files\java\jdk1.8.0_101\jre October 13, 2016 8:58:29 am Org.apache.cat Alina.startup.VersionLoggerListener Log Info: JVM version:1.8.0_101-b13 October 13, 2016 8:58:29 am Org.apache.catalin A.startup.versionloggerlistener log information: JVM vendor:oracle Corporation October 13, 2016 8:58:29 am Org.apache.catali Na.startup.VersionLoggerListener Log Info: Catalina_base:d:\program files\jr-tomcat-7.0.68 October 13, 2016 8:58:29 am O Rg.apache.catalina.startup.VersionLoggerListener log information: Catalina_home:d:\program files\jr-tomcat-7.0.68 October 13, 2 016 8:58:29 am Org.apache.catalina.startup.VersionLoggerListener Log info: Command line argument:- dmaven.multimoduleprojectdirectory= $M 2_home October 13, 2016 8:58:29 am Org.apache.catalina.startup.VersionLoggerListener Log info: Command line argument:-dcatalina.base=d:\program files\jr-tomcat-7.0.68 October 13, 2016 8:58:29 am Org.apache.catalin A.startup.versionloggerlistener Log message: Command line ARgument:-dcatalina.home=d:\program files\jr-tomcat-7.0.68 October 13, 2016 8:58:29 a.m. Org.apache.catalina.startup.VersionLoggerListener Log message: Command line argument:-dwtp.deploy=d:\program files\ Jr-tomcat-7.0.68\webapps October 13, 2016 8:58:29 am org.apache.catalina.startup.VersionLoggerListener log info: Command line ar Gument:-djava.endorsed.dirs=d:\program files\jr-tomcat-7.0.68\endorsed October 13, 2016 8:58:29 a.m. Org.apache.catalina.startup.VersionLoggerListener Log message: Command line argument:-dfile.encoding=utf-8 October 13, 2016 8:58:29 am Org.apache.catalina.core.AprLifecycleListener lifecycleevent Info: The APR based Apache Tomcat Native library WHI CH allows optimal performance in production environments is not found on the Java.library.path:d:\program files\java\jdk 1.8.0_101\bin; C:\Windows\Sun\Java\bin; C:\Windows\system32; C:\Windows; C:\Windows\system32; C:\Windows; C:\Windows\System32\Wbem; C:\Windows\System32\WindowsPowerShell\v1.0\; C:\Program Files (x86) \ Windows imaging\; C:\Program Files (x86) \Amd\ati. ace\core-static; C:\Program Files (x86) \ati Technologies\ati. ace\core-static; C:\Program Files\java\jdk1.7.0_60\bin;
C:\Program Files\tortoisesvn\bin;. October 13, 2016 8:58:29 am org.apache.coyote.AbstractProtocol init info: Initializing protocolhandler ["http-bio-8080"] October 13, 2 016 8:58:29 am ORG.APACHE.COYOTE.ABSTRACTPROTOCOL init info: Initializing protocolhandler ["ajp-bio-8009"] October 13, 2016 8:58: 29 am Org.apache.catalina.startup.Catalina Load Info: Initialization processed in 662 Ms October 13, 2016 8:58:29 am org.apache.c Atalina.core.StandardService startinternal Info: Starting service Catalina October 13, 2016 8:58:29 am Org.apache.catalina.core. Standardengine startinternal Info: Starting Servlet engine:apache tomcat/7.0.68 October 13, 2016 8:58:29 morning Org.apache.catalina. Loader. Webappclassloaderbase validatejarfile Info: validatejarfile (D:\program files\jr-tomcat-7.0.68\webapps\ecf.meloans\ Web-inf\lib\servlet-api-2.5.jar)-jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending Class:javax/serVlet/servlet.class October 13, 2016 8:58:32 am org.apache.catalina.startup.TldConfig Execute info: At least one JAR is scanned f or TLDs yet contained no TLDs. Enable debug logging for the logger for a complete list of JARs that were scanned but no T LDs were found in them.
Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. October 13, 2016 8:58:32 am org.apache.catalina.core.ApplicationContext log info: Spring webapplicationinitializers detected on C Lasspath: [COM.MIDEA.JR.ECF.MELOANS.UTILS.JRLOGBACKCONFIGLOADER@11ED90A7] Set system logging parameter: Log.app.version = 0.0.1 Set system logging Parameter:log.dir = D:/wangzy25/log Set system logging PARAMETER:LOG.CREDIT.A Ccounting.level = ERROR Set system logging Parameter:log.credit.accounting.task.level = DEBUG Set system logging Paramete R:LOG.APP.ENV = Development set system logging Parameter:log.credit.accounting.dao.level = ERROR Set system logging Para Meter:log.root.level = DEBUG Set system logging pArameter:log.app.name = Credit.accounting Set system logging Parameter:log.system.name = COM.MIDEA.JR October 13, 2016 8:58:3 2 am org.apache.catalina.core.ApplicationContext Log info: Initializing Spring root webapplicationcontext log4j:warn No app
Enders could is found for logger (org.springframework.web.context.ContextLoader).
Log4j:warn Initialize the log4j system properly.
Log4j:warn See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Slf4j:class path contains multiple slf4j bindings.

which

Info: Starting service Catalina
...
Info: Spring webapplicationinitializers detected on classpath: [com.midea.jr.ecf.meloans.utils.jrlogbackconfigloader@ 11ED90A7]
...
Info: Initializing Spring root Webapplicationcontext
Slf4j:class path contains multiple slf4j bindings.

Represents the Catalina start-->webapplicationinitializers-->webapplicationcontext-->slf4j bindings

So we just load the configuration file before slf4j bindings.


Scenario 1, by configuring listener, before placing it in Logback

Package Com.midea.jr.ecf.meloans.listener;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.Enumeration;

Import java.util.Properties;
Import javax.servlet.ServletContextEvent;

Import Javax.servlet.ServletContextListener;


Import Ch.qos.logback.ext.spring.web.WebLogbackConfigurer; /** * &LT;P&GT;PROJECT:COM.MIDEA.JR.ECF <p> * <p>module:meloans.utils <p> * <p>description: Load System Profile system.properties <p> * * @author WANGZY25 * @date October 12, 2016 PM 3:03:54 */public class Loadsystemcon 
		Figlistener implements Servletcontextlistener {@Override public void contextinitialized (Servletcontextevent sce) {
		Properties prop = new properties ();
		InputStream in = This.getclass (). getResourceAsStream ("/system.properties");
		SYSTEM.OUT.PRINTLN ("*********** Begin loading Log configuration *********");
			try {prop.load (in);
			Enumeration<object> iter= Prop.keys (); while (Iter.hasmoreelements ()) {String key = Iter.nextelement (). toString ();
					if (Key.startswith ("log")) {System.setproperty (key, Prop.getproperty (key));
				SYSTEM.OUT.PRINTLN ("Set system logging parameter:" +key+ "=" +prop.getproperty (key) ");
		}}} catch (IOException e) {e.printstacktrace ();
	} weblogbackconfigurer.initlogging (Sce.getservletcontext ()); } @Override public void contextdestroyed (Servletcontextevent sce) {weblogbackconfigurer.shutdownlogging (sce.getser
	Vletcontext ());
 }
}

Also in Web. XML, configure the listener to the front position

<listener>
        <listener-class>com.midea.jr.ecf.meloans.utils.loadsystemconfiglistener</ Listener-class>
</listener>


Scenario 2, overriding Webapplicationinitializer

Package Com.midea.jr.ecf.meloans.listener;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.Enumeration;

Import java.util.Properties;
Import Javax.servlet.ServletContext;

Import javax.servlet.ServletException;


Import Org.springframework.web.WebApplicationInitializer; /** * &LT;P&GT;PROJECT:COM.MIDEA.JR.ECF <p> * <p>module:meloans.utils <p> * <p>description: Load System Profile system.properties <p> * * @author WANGZY25 * @date October 12, 2016 PM 3:03:54 */public class jrlogbackconf  Igloader implements webapplicationinitializer{@Override public void Onstartup (ServletContext servletcontext) throws
		Servletexception {Properties prop = new Properties ();
		InputStream in = This.getclass (). getResourceAsStream ("/system.properties");
			try {prop.load (in);
			Enumeration<object> iter= Prop.keys ();
				while (Iter.hasmoreelements ()) {String key = Iter.nextelement (). toString (); if (Key.startswith ("log")) {SysteM.setproperty (Key, Prop.getproperty (key));
				SYSTEM.OUT.PRINTLN ("Set system logging parameter:" +key+ "=" +prop.getproperty (key) ");
		}}} catch (IOException e) {e.printstacktrace ();
 }
	}

}

Reference Address:

logback Unified Configuration and environment variable loading problem HTTP://WWW.JIANSHU.COM/P/39178AF66AEF

"Flying the Moon" analysis Logback2:logback start

http://feitianbenyue.iteye.com/blog/2205631

diagram of the Tomcat class loading mechanism

Http://www.cnblogs.com/xing901022/p/4574961.html

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.