Announcement Announcement Module
Collapse
No announcement yet.
Problem with Log4jConfigListener Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with Log4jConfigListener

    Hello,
    im trying to setup log4j in my web.xml for a webservice running in tomcat. Here's my web.xml:
    Code:
    <web-app>
      <display-name>Archetype Created Web Application</display-name>
    
      <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/spring-context.xml</param-value>
        </context-param>
        
        <context-param>
            <param-name>log4jExposeWebAppRoot</param-name>
            <param-value>false</param-value>
        </context-param>
       
       <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>${config.dir}/log4j.xml</param-value>
        </context-param>   
          
        
        <listener>
            <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
        </listener>
        
         <listener>
            <listener-class>
                org.springframework.web.context.ContextLoaderListener
            </listener-class>
        </listener>
        <servlet>
            <servlet-name>CXFServlet</servlet-name>
            <display-name>CXF Servlet</display-name>
            <servlet-class>
                org.apache.cxf.transport.servlet.CXFServlet
            </servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>CXFServlet</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    </web-app>
    It is a requirement in my current project to configure the location of various configfiles (including log4j.xml) by a Java System property. When i replace the
    Code:
    ${config.dir}/log4j.xml
    line with the actual path everything works fine. But using the property replacement i run into errors because the replace property is prefixed with the path to the current webapp like this:

    Code:
    log4j:WARN No appenders could be found for logger (org.apache.commons.digester.Digester.sax).
    log4j:WARN Please initialize the log4j system properly.
    log4j:ERROR Could not open [file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%205.5/webapps/wrapper_service/C:/Developement/eclipse-ws/LegoBricksWrapper/log4j.xml].
    java.io.FileNotFoundException: C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\wrapper_service\C:\Developement\eclipse-ws\myApp\log4j.xml (The filename, directory name, or volume label syntax is incorrect)
    	at java.io.FileInputStream.open(Native Method)
    	at java.io.FileInputStream.<init>(Unknown Source)
    	at java.io.FileInputStream.<init>(Unknown Source)
    	at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
    	at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)
    	at java.net.URL.openStream(Unknown Source)
    	at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:602)
    	at org.apache.log4j.xml.DOMConfigurator.configure(DOMConfigurator.java:697)
    	at org.springframework.util.Log4jConfigurer.initLogging(Log4jConfigurer.java:70)
    	at org.springframework.web.util.Log4jWebConfigurer.initLogging(Log4jWebConfigurer.java:152)
    	at org.springframework.web.util.Log4jConfigListener.contextInitialized(Log4jConfigListener.java:47)
    	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3764)
    	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4216)
    	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
    	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)
    	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
    	at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:831)
    	at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:720)
    	at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:490)
    	at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1150)
    	at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
    	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)
    	at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
    	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    	at org.apache.catalina.core.StandardService.start(StandardService.java:448)
    	at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
    	at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
    	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:295)
    	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
    Any clues Whats going wrong here?
    Thanks in advance for any hints,
    Johannes

  • #2
    I actually figured it out in a little debugging session:
    This is what Log4JWebConfigurer does:
    Code:
    	// Return a URL (e.g. "classpath:" or "file:") as-is;
    				// consider a plain file path as relative to the web application root directory.
    				if (!ResourceUtils.isUrl(location)) {
    					// Resolve system property placeholders before resolving real path.
    					location = SystemPropertyUtils.resolvePlaceholders(location);
    					location = WebUtils.getRealPath(servletContext, location);
    				}
    So one has to prefix ones absolute path with a "file:" .

    Cheers, Johannes.

    Comment


    • #3
      Hi,

      is there a specific reason, why Log4jWebConfigurer only resolves
      placeholders, if the config location value is not a URL?
      Why doesn't it try to always resolve placeholders first?

      As far as I know, replacement of environment variables in web.xml is a Tomcat-specific feature, so using something like
      Code:
      ${config.dir}/log4j.xml
      as config location is currently not guaranteed to work in other servlet containers, right?
      If Log4jWebConfigurer would do it's own placeholder replacement first, it should work, if I'm not mistaken...

      Ideally, handling and transforming of the config location should be pluggable in my opinion.

      Thanks,
      Holger

      Comment


      • #4
        I accept with code:
        <web-app>
        <display-name>Archetype Created Web Application</display-name>

        <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring-context.xml</param-value>
        </context-param>

        <context-param>
        <param-name>log4jExposeWebAppRoot</param-name>
        <param-value>false</param-value>
        </context-param>

        <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>${config.dir}/log4j.xml</param-value>
        </context-param>


        <listener>
        <listener-class>org.springframework.web.util.Log4jConfigList ener</listener-class>
        </listener>
        ________________________
        Cheap tennis ball machine for sale | Used lobster tennis ball machine | Review of Tennis twist, Playmate, Little Prince and Wilson tennis ball machines
        Last edited by gigicso; Oct 13th, 2009, 06:28 AM.

        Comment

        Working...
        X