log4j log file configuration relative path in Spring MVC

Source: Internet
Author: User

log4j and Web. XML Configuration Webapprootkey Issues

1 in the Web. XML configuration

<context-param>
<param-name>webAppRootKey</param-name>
<param-value>web.sample.root</param-value>
</context-param>

You can use System.getproperty ("web.sample.root") to get the property value . When you debug a Web project in Eclipse, the path to the project is a temporary path , not the real path, and you can print out the attribute values from the above statement to see where the temporary project path

such as: System.out.println ("web.root:" + system.getproperty ("Web.root"));

Output: web.root:D:\apache-tomcat-6.0.30\webapps\wangzun\

2, spring through Org.springframework.web.util.WebAppRootListener this listener to press into the project path. However, if the Org.springframework.web.util.Log4jConfigListener listener is already configured in Web. XML, you do not need to configure Webapprootlistener. Because Log4jconfiglistener already contains the functions of Webapprootlistener.

Configuration Webapprootlistener:

<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>


3. Multiple Web projects deployed in the same container, to configure different <param-value>, cannot repeat webapprootkey system variable name

4.Webapprootlistener before ApplicationContext's Contextloaderlistener , Otherwise, an ApplicationContext exception occurs when the bean is injected into the root directory value.

<!--project root directory Listener
<listener>
<listener-class>org.springframework.web.util. Webapprootlistener</listener-class>
</listener>
<!--spring ApplicationContext-Loaded
<listener>
<listener-class>org.springframework.web.context. Contextloaderlistener</listener-class>
</listener>


4, if you configure the
Log4j.appender.file.file=${web.sample.root}web-inf/logs/sample.log

LOG4J will automatically build the logs directory itself, without the need to manually explicitly create an empty logs directory


When deploying two or more projects under Tomcat, it is best to define the Webapprootkey parameter in the Web. xml file, if not defined,will default to "Webapp.root",As follows:

<!--application Path--
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>Webapp.root</param-value>
</context-param>
Best newspaper The parameter values for each item are different to avoid conflicting items

Severity: Exception sending context initialized event to listener instance of class Org.springframework.web.util.Log4jConfigLis Tener
Java.lang.IllegalStateException:Web App root system property already set to different value: ' Webapp.root ' = [C:\Program Files (x86) \apache software Foundation\tomcat 6.0\webapps\drmproject\] instead of [C:\Program Files (x86) \apache Software Foundation\tomcat 6.0\webapps\drmsn\]-Choose unique values for the ' Webapprootkey ' Context-param in your WEB.XM L files!

For multiple projects to be configured for Webapprootkey,this is mainly to allow log to write to the corresponding project root directory, as I configured these two items for the Webapprootkey
Item 1 of Web. xml:

<!--application Path--
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root1</param-value>
</context-param>

<!--project root directory Listener
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>

Item 2 of Web. xml:
<!--application Path--
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>Webapp.root2</param-value>
</context-param>

 <!--project root directory listener-->
 <listener>
  <listener-class> Org.springframework.web.util.webapprootlistener</listener-class>
 </listener>

So there will be no conflict. After the
definition, ${webname.root} to represent the absolute path of the Web directory, the log file is stored in the WebApp.

Namemax:

<context-param>
<param-name>webAppRootKey</param-name>
<param-value>web.root</param-value>
</context-param>

<!--project root directory Listener
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>

Beans can be used in:

<bean id= "Transformchinese" class= "Com.zunmi.util.TransformChinese"

P:outbasepath= "${web.root}web-inf/destinedomainfile/"
P:j2fsource= "${web.root}web-inf/simpletotraditional.properties"
p:charset= "GBK"
/>


log4j log file configuration relative path in Spring MVC

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.