DaveyBob
DaveyBob

Reputation: 253

Same webapp on Tomcat with different log4j config for each

I've hunted and read, and I think there's no way...but just in case.

I need to deploy the same webapp to Tomcat multiple times, each with a different config that indicates which database to work with. I've figured out how to do that without modifying the contents of the war file.

In short, I copied WebApp.jar to WebAppDB1.jar, WebAppDB2.jar, and deploy these to the webapps folder, and use a custom context configuration for each in tomcat/conf/Catalina/localhost. So I'm good there.

But I would really like for each of these to log to a separate file. As it is, everything goes to catalina.out. It's a Spring app using log4j and slf4j. Every avenue I've explored gets me nowhere.

For now, I'm back to updating the actual war file, going into WEB-INF/classes and updating log4j.xml, but that makes it a manual process.

Any ideas?

Upvotes: 0

Views: 1177

Answers (1)

Shinichi Kai
Shinichi Kai

Reputation: 4523

You can put the log4j.xml files somewhere in the outside the webapp and use Tomcat's VirtualWebappLoader to load different log4.xml for each webapps.

http://tomcat.apache.org/tomcat-7.0-doc/config/loader.html#VirtualWebappLoader_Implementation

It looks like this.

${CATALINA_BASE}/conf/Catalina/localhost/WebAppDB1.xml:

<Context docBase="..." >
  <Loader className="org.apache.catalina.loader.VirtualWebappLoader" virtualClasspath="/somewhere/WebAppDB1" />
</Context>

${CATALINA_BASE}/conf/Catalina/localhost/WebAppDB2.xml:

<Context docBase="..." >
  <Loader className="org.apache.catalina.loader.VirtualWebappLoader" virtualClasspath="/somewhere/WebAppDB2" />
</Context>

And then put log4j.xml files into /somewhere/WebAppDB1 and /somewhere/WebAppDB2.

Hope this helps.

Upvotes: 1

Related Questions