In order for spring in a Web project to use log4j to do the following configuration:
1. Add the following in Web. xml:
<!--If the Webapprootkey parameter is not defined, then Webapprootkey is the default "Webapp.root". But it's best to set up to avoid name collisions between projects.
If I have two projects here that are not configured with Web. Webapprootkey, then these two items will appear with the following exception when they are published to Tomcat:
XML code
- 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 ' = [D:\apache-t Omcat-6.0.29\webapps\uploadanddownload\] instead of [D:\apache-tomcat-6.0.29\webapps\w\]-Choose unique values for the ' Webapprootkey ' Context-param in your web. XML files!
- At Org.springframework.web.util.WebUtils.setWebAppRootSystemProperty (webutils.java:146)
- At Org.springframework.web.util.Log4jWebConfigurer.initLogging (log4jwebconfigurer.java:118)
- At Org.springframework.web.util.Log4jConfigListener.contextInitialized (log4jconfiglistener.java:47)
- At Org.apache.catalina.core.StandardContext.listenerStart (standardcontext.java:4135)
- At Org.apache.catalina.core.StandardContext.start (standardcontext.java:4630)
- At Org.apache.catalina.core.ContainerBase.addChildInternal (containerbase.java:791)
- At Org.apache.catalina.core.ContainerBase.addChild (containerbase.java:771)
- At Org.apache.catalina.core.StandardHost.addChild (standardhost.java:546)
- At Org.apache.catalina.startup.HostConfig.deployDirectory (hostconfig.java:1041)
- At Org.apache.catalina.startup.HostConfig.deployDirectories (hostconfig.java:964)
- At Org.apache.catalina.startup.HostConfig.deployApps (hostconfig.java:502)
- At Org.apache.catalina.startup.HostConfig.start (hostconfig.java:1277)
- At Org.apache.catalina.startup.HostConfig.lifecycleEvent (hostconfig.java:321)
- At Org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent (lifecyclesupport.java:119)
- At Org.apache.catalina.core.ContainerBase.start (containerbase.java:1053)
- At Org.apache.catalina.core.StandardHost.start (standardhost.java:785)
- At Org.apache.catalina.core.ContainerBase.start (containerbase.java:1045)
- At Org.apache.catalina.core.StandardEngine.start (standardengine.java:445)
- At Org.apache.catalina.core.StandardService.start (standardservice.java:519)
- At Org.apache.catalina.core.StandardServer.start (standardserver.java:710)
- At Org.apache.catalina.startup.Catalina.start (catalina.java:581)
- At Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
- At Sun.reflect.NativeMethodAccessorImpl.invoke (Unknown Source)
- At Sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown Source)
- At Java.lang.reflect.Method.invoke (Unknown Source)
- At Org.apache.catalina.startup.Bootstrap.start (bootstrap.java:289)
- At Org.apache.catalina.startup.Bootstrap.main (bootstrap.java:414)
- 2010-10-10 17:18:18 org.apache.catalina.core.StandardContext Start
- Severity: Error Listenerstart
So for more than one project to configure the Webapprootkey, here is to let log can write to the corresponding project root directory, as I configured these two projects Webapprootkey
XML code
- <!--application Path--
- <context-param>
- <param-name>webAppRootKey</param-name>
- <param-value>spring.webapp.root</param-value>
- </context-param>
XML code
- <!--application Path--
- <context-param>
- <param-name>webAppRootKey</param-name>
- <param-value>spring.webapp.root2</param-value>
- </context-param>
So there will be no conflict.
Once defined, the absolute path to root is written to the system variable when the Web container is started.
You can then use ${webname.root} in the log4j configuration file to represent the absolute path to the Web directory and to store the log file in WebApp.
XML code
- <!--This parameter is used for the following "Log4jconfiglistener"-
- <context-param>
- <param-name>webAppRootKey</param-name>
- <param-value>webName.root</param-value>
- </context-param>
- <!--log4j configuration file loaded by Sprng--
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>/WEB-INF/log4j.properties</param-value>
- </context-param>
- <!--spring Refreshes the log4j configuration file interval by default in millisecond-->
- <context-param>
- <param-name>log4jRefreshInterval</param-name>
- <param-value>60000</param-value>
- </context-param>
- <!--WEB Project Spring load log4j--
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
2. Write the Log4j.properties property file, using the global variables configured with Web. XML:
Log4j.appender.A_default. File=${webname.root}/web-inf/logs/log4j.log
The details are slightly more informative.
Problem:
As configured above, the Log4j.properties property file should be stored in the WEB project under Web-inf instead of the default/web-inf/classes.
You can do the spring unit test, the default read classes under the Log4j.properties, do not know how to solve the problem?
If the log4j.properties is stored under/web-inf/classes/log4j.properties, it will be wrong to start the Tomcat times:
Log4j:error Setfile (null,true) call failed.
Java.io.FileNotFoundException: \web-inf\logs\log4j.log (the system cannot find the path specified.) )
You can see in \web-inf\logs\log4j.log that the Log4j.log file already exists in spring's log output information.
Parsing may be the value of the ${webname.root} variable that cannot be read by other classes when loading log4j.properties.
Webapprootkey parameter issues with log4j configuration