I'll teach you the springframe. log4j Configuration Source Code Analysis

Source: Internet
Author: User
Tags log4j
In the Petclinic project, there are several entries in Web.xml that are related to log4j, which are:


1.
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>petclinic.root</param-value>
</context-param>


2.
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INFclasseslog4j.properties</param-value>
</context-param>




3. (The entry is commented out in the Petclinic)
<listener>
<listener- class>org.springframework.web.util.log4jconfiglistener</listener- class>
</listener>



We know that log4j is used in web-application, and there are many ways to configure it:
A.
Configure the log4j with a servlet or Servletcontextlistener. Typically, you don't have to write a servlet or listener yourself, like using Log4j's Com.apache.jakarta.log4j.Log4jInit class directly, Spring's Org.springframework.web.util.Log4jConfigServlet and Org.springframework.web.util.Se
Rvletcontextlistener, this configuration is flexible, you can specify the location of Log4j.properties by parameter entry.

B.
Place the log4j default profile (log4j.properties) in the classpath, usually in the/web-inf/classes directory. This is the log4j default configuration, no other configuration. The disadvantage is that you cannot flexibly specify the Log4j.properties file location through the configuration file. In our Petclinic project, because listener entries are annotated, this is also the default method.


Now we consider the case where the listener entry is not annotated, which is the same as the purpose of registering log4jconfigservlet, except that it must be used in the servlet container that supports listener.

Find the source code for Log4jconfigservlet and Servletcontextlistener, and they all call log4jwebconfigurer.initlogging in the right place (callback method) ( Getservletcontext ()); Navigate to this method, the first sentence is: Webutils.setwebapprootsystemproperty (ServletContext), and then navigate to the method, the method is very short:





Public Static voidSetwebapprootsystemproperty (ServletContext ServletContext) throws illegalstateexception{
Stringparam = Servletcontext.getinitparameter (web_app_root_key_param);
StringKey = (param!= NULL? Param:default_web_app_root_key);
StringOldValue = System. GetProperty (key);
if(OldValue!= NULL) {
Throw New illegalstateexception("Warning:web app root system property already set:" + key + "=" +


OldValue + "-Choose unique Webapprootkey values in your Web.xml files!");
}
StringRoot = Servletcontext.getrealpath ("/");
if(Root = = NULL) {
Throw New illegalstateexception("Cannot set web App root system" when WAR file isn't

Expanded ");
}
System. setProperty (key, Root);
ServletContext.log ("Set Web App root system property:" + key + "=" + root);
}




From the code, this method actually saves the absolute file path of the root directory of the Web application as a property in the system's list of properties. The name of the property, indicated by the value of the parameter named "Webapprootkey" in the Web.xml file. If you do not define the Webapprootkey parameter in Web.xml, the property name is the default "Webapp.root". The Webapprootkey parameter has been defined in our Petclinic project with the value "Petclinic.root" , so the name of the attribute is "Petclinic.root".

Then go back to Log4jwebconfigurer.initlogging (Getservletcontext ()) and the next act is to get from the Web.xml Log4jconfiglocation and Log4jrefreshinterval. The former indicates the location and name of the log4j configuration file (possibly in XML format), The latter indicates the time interval at which the configuration file is to be fetched again. Then call the Log4jconfigurer.initlogging () method to configure the log4j. From the Log4jconfigurer.initlogging () method we can see that for different formats of configuration files (properties or XML), Log4jconfigurer are configured with different lo4j configurator classes, such as Domconfigurator or Propertyconfigurator.

So far, we have basically made it clear about the configuration of log4j in Petclinic. But in the system object
When is the Petclinic.root attribute used? In the log4j.properties file below web-inf/classes, there is one sentence:
Log4j.appender.logfile.file=${petclinic.root}/web-inf/petclinic.log
In this way, we use the Petclinic.root attribute. From the above analysis, if you do not define Webapprootkey parameters in the Web.xml, then you have to put log4j.appender.logfile.file=${petclinic.root}/web-inf/ Petclinic.log
Change the petclinic.root variable in the webapp.root variable or simply replace it with another absolute path.

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.