Webapprootkey parameter issues with LOG4J configuration

Source: Internet
Author: User

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
  1. Severity: Exception sending context initialized event to listener instance of class Org.springframework.web.util.Log4jConfigLis Tener
  2. 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!
  3. At Org.springframework.web.util.WebUtils.setWebAppRootSystemProperty (webutils.java:146)
  4. At Org.springframework.web.util.Log4jWebConfigurer.initLogging (log4jwebconfigurer.java:118)
  5. At Org.springframework.web.util.Log4jConfigListener.contextInitialized (log4jconfiglistener.java:47)
  6. At Org.apache.catalina.core.StandardContext.listenerStart (standardcontext.java:4135)
  7. At Org.apache.catalina.core.StandardContext.start (standardcontext.java:4630)
  8. At Org.apache.catalina.core.ContainerBase.addChildInternal (containerbase.java:791)
  9. At Org.apache.catalina.core.ContainerBase.addChild (containerbase.java:771)
  10. At Org.apache.catalina.core.StandardHost.addChild (standardhost.java:546)
  11. At Org.apache.catalina.startup.HostConfig.deployDirectory (hostconfig.java:1041)
  12. At Org.apache.catalina.startup.HostConfig.deployDirectories (hostconfig.java:964)
  13. At Org.apache.catalina.startup.HostConfig.deployApps (hostconfig.java:502)
  14. At Org.apache.catalina.startup.HostConfig.start (hostconfig.java:1277)
  15. At Org.apache.catalina.startup.HostConfig.lifecycleEvent (hostconfig.java:321)
  16. At Org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent (lifecyclesupport.java:119)
  17. At Org.apache.catalina.core.ContainerBase.start (containerbase.java:1053)
  18. At Org.apache.catalina.core.StandardHost.start (standardhost.java:785)
  19. At Org.apache.catalina.core.ContainerBase.start (containerbase.java:1045)
  20. At Org.apache.catalina.core.StandardEngine.start (standardengine.java:445)
  21. At Org.apache.catalina.core.StandardService.start (standardservice.java:519)
  22. At Org.apache.catalina.core.StandardServer.start (standardserver.java:710)
  23. At Org.apache.catalina.startup.Catalina.start (catalina.java:581)
  24. At Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
  25. At Sun.reflect.NativeMethodAccessorImpl.invoke (Unknown Source)
  26. At Sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown Source)
  27. At Java.lang.reflect.Method.invoke (Unknown Source)
  28. At Org.apache.catalina.startup.Bootstrap.start (bootstrap.java:289)
  29. At Org.apache.catalina.startup.Bootstrap.main (bootstrap.java:414)
  30. 2010-10-10 17:18:18 org.apache.catalina.core.StandardContext Start
  31. 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
    1. <!--application Path--
    2. <context-param>
    3. <param-name>webAppRootKey</param-name>
    4. <param-value>spring.webapp.root</param-value>
    5. </context-param>

XML code
    1. <!--application Path--
    2. <context-param>
    3. <param-name>webAppRootKey</param-name>
    4. <param-value>spring.webapp.root2</param-value>
    5. </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
  1. <!--This parameter is used for the following "Log4jconfiglistener"-
  2. <context-param>
  3. <param-name>webAppRootKey</param-name>
  4. <param-value>webName.root</param-value>
  5. </context-param>
  6. <!--log4j configuration file loaded by Sprng--
  7. <context-param>
  8. <param-name>log4jConfigLocation</param-name>
  9. <param-value>/WEB-INF/log4j.properties</param-value>
  10. </context-param>
  11. <!--spring Refreshes the log4j configuration file interval by default in millisecond-->
  12. <context-param>
  13. <param-name>log4jRefreshInterval</param-name>
  14. <param-value>60000</param-value>
  15. </context-param>
  16. <!--WEB Project Spring load log4j--
  17. <listener>
  18. <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  19. </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

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.