I just hacked together a quick example on Windows/XP Professional. I imagine it 
will work the same way on my Linux machine.

Environment:

OS:     Windows/XP Professional - all the latest patches
Java:   1.6.0_20 from Sun
Tomcat: 6.0.26
IDE:    NetBeans 6.8

I am reworking the Spring Developer's Notebook bike store example, so I can 
understand Spring testing and using mock objects. I've run into a lot of 
"interesting" issues, mostly having to do with Spring test libraries versus 
Junit versus mock objects. However, that's a topic for another discussion.

At any rate, I set up the following system:

Tomcat location:   C:\Apache\apache-tomcat-6.0.26
localhost webapps: C:\Apache\apache-tomcat-6.0.26\webapps
foo webapps:       C:\Apache\hosts\foo-hosts\webapps
bar webapps:       C:\Apache\hosts\bar-hosts\webapps

I like to put my virtual hosts outside of the Tomcat directory tree. That way, 
when I upgrade Tomcat I don't have to worry about virtual host applications.

Next, I duplicated the <Host></Host> node in server.xml for the foo and bar 
hosts. The resulting <Host></Host> nodes from my server.xml are shown below.

<Host name="localhost"  appBase="webapps"
      unpackWARs="true" autoDeploy="true"
      xmlValidation="false" xmlNamespaceAware="false">
      </Host>

<Host name="foo"  appBase="C:/Apache/hosts/foo-host/webapps"
      unpackWARs="true" autoDeploy="true"
      xmlValidation="false" xmlNamespaceAware="false">
</Host>

<Host name="bar"  appBase="C:/Apache/hosts/bar-host/webapps"
      unpackWARs="true" autoDeploy="true"
      xmlValidation="false" xmlNamespaceAware="false">
</Host>

Note that I didn't change any of the logging prefixes and suffixes, so all 
common logging gets mixed together. Since this is just a proof of concept, I 
don't really mind.

Next, I copied manager.xml from conf/Catalina/localhost to conf/Catalina/foo, 
and conf/Catalina/bar.

I also copied the manager and ROOT web applications to foo-host\webapps and 
bar-host\webapps (full path above). It's nice to have a generic welcome page on 
test hosts just to make sure your servers are running. The manager application 
is useful as well, although I can just copy war files around.

Finally for my setup, I added 127.0.0.1 foo and 127.0.0.1 bar to my hosts file 
in WINNT\system32\drivers\etc (this is an upgraded Windows/2000 Professional 
machine).

Upon starting up this mess, all three hosts were visible at localhost:8080, 
foo:8080, and bar:8080 as you would expect.

I then took the web application, and created three war files. The only 
difference in each of the war files was the location of the log file. An 
example is shown below.

### direct messages to file dnb-02.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=${catalina.home}/logs/dnb-02.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=warn, file

### Spring Framework logging
log4j.logger.org.springframework=info

For the other two war files, I just changed dnb-02.log to foo-dnb-02.log and 
bar-dnb-02.log.

I really should use a different environment variable and pass it in via 
JAVA_OPTS. That way I can set it easily in my IDE to do testing that will 
include logging.

I then used the Tomcat manager on all three virtual hosts to load up the 
appropriate applications.

To test, I caused different warnings to be logged in different virtual hosts, 
then grepped the logs for the results.

As expected, I got three different log files, and the warnings (along with time 
stamps) were in the right log files.

Note that I do not have any non-Tomcat jar in apache-tomcat-6.0.26 besides JDBC 
jars at this point.

Just for completeness - here's the web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
  xmlns="http://java.sun.com/xml/ns/javaee";
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd";>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>
          org.springframework.web.context.ContextLoaderListener
       </listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
          org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

Sorry for the formatting.

Hope this helps. I hate to say this . . . it works for me.

/mde/

--- On Tue, 5/4/10, Joe Hansen <joe.hansen...@gmail.com> wrote:

> From: Joe Hansen <joe.hansen...@gmail.com>
> Subject: Re: Log4j logging doesn't work when a web application is moved  
> outside Tomcat/webapps directory
> To: "Tomcat Users List" <users@tomcat.apache.org>
> Date: Tuesday, May 4, 2010, 4:55 PM
> This thread has grown too long. For
> anyone following this thread,
> here's the summary:
> 
> Two web applications, each with a Virtual Host entry in
> server.xml file.
> Tomcat/webapps-abc
> Tomcat/webapps-xyz
> 
> These two web applications have different log4j.properties
> files.
> These log4j property files log to different log files
> (Tomcat/logs/abc.log and Tomcat/logs/xyz.log).
> Tomcat/webapps-abc/ROOT/WEB-INF/classes/log4j.properties
> Tomcat/webapps-xyz/ROOT/WEB-INF/classes/log4j.properties
> 
> The web applications abc and xyz using Java Commons Logging
> API (Log
> and LogFactory classes) to perform logging.
> 
> Scenario 1:
> log4j.jar is in Tomcat/lib directory
> log4j.jar is NOT in WEB-INF/lib directories
> 
> Result:
> abc webapp's log entries are written to xyz.log file
> 
> 
> Scenario 2:
> log4j.jar is NOT in Tomcat/lib directory
> log4j.jar is in WEB-INF/lib directories
> 
> Result:
> ClassNotFoundError in Tomcat/logs/catalina.log files
> 
> 
> Scenario 3:
> log4j.jar is in Tomcat/lib directory
> log4j.jar is in WEB-INF/lib directories
> 
> Result:
> Empty abc.log and xyz.log files.
> 
> 
> Here's the relevant portion of the
> webapps-abc/ROOT/WEB-INF/web.xml file:
>     <listener>
>        
> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
>     </listener>
>     <context-param>
>        
> <param-name>log4jConfigLocation</param-name>
>        
> <param-value>/WEB-INF/classes/log4j.properties</param-value>
>     </context-param>
>     <context-param>
>        
> <param-name>log4jExposeWebAppRoot</param-name>
>        
> <param-value>false</param-value>
>     </context-param>
>     <listener>
>        
> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
>       </listener>
> 
> 
>  webapps-xyz/ROOT/WEB-INF/web.xml file looks just the same
> 
> /* webapps-abc/ROOT/WEB-INF/classes/log4j.properties */
> log4j.rootLogger=INFO, A2
> log4j.appender.A2=org.apache.log4j.RollingFileAppender
> log4j.appender.A2.layout=org.apache.log4j.PatternLayout
> log4j.appender.A2.layout.ConversionPattern=%d{yyyy MMM dd /
> HH:mm:ss}
> %-5p - [%c] : %m %n
> log4j.appender.A2.File=c:/Tomcat6/logs/abc.log
> log4j.appender.A2.MaxFileSize=100KB
> log4j.appender.A2.MaxBackupIndex=4
> 
> 
> /* webapps-xyz/ROOT/WEB-INF/classes/log4j.properties */
> log4j.rootLogger=INFO, A1
> log4j.appender.A1=org.apache.log4j.RollingFileAppender
> log4j.appender.A1.layout=org.apache.log4j.PatternLayout
> log4j.appender.A1.layout.ConversionPattern=%d{yyyy MMM dd /
> HH:mm:ss}
> %-5p - [%c] : %m %n
> log4j.appender.A1.File=c:/Tomcat6/logs/xyz.log
> log4j.appender.A1.MaxFileSize=100KB
> log4j.appender.A1.MaxBackupIndex=4
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org
> 
> 





---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to