On 9/17/2012 9:31 AM, Christopher Schultz wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Izolda,

On 9/16/12 6:19 AM, Kiss Izolda wrote:
I have configured Tomcat to use log4j according to
http://tomcat.apache.org/tomcat-7.0-doc/logging.html I have used
the same log4j.properties. My web application is using log4j as
well. There is no log4j.jar in WEB-INF/lib.

You need to put one in there if you want application-level logging to
work the way you expect.

How can I prevent Tomcat from using the web application's root
logger?

Tomcat will not use the web application's logger if you configure the
two logging systems separately. You need:

- - log4j.jar in CATALINA_BASE/lib (for Tomcat's logging)
- - log4j.xml in CATALINA_BASE/lib (for Tomcat's logging config)
- - log4j.jar in webapp's WEB-INF/lib (for app logging)
- - log4j.xml in webapp's WEB-INF/lib (for app logging config)

Also, remember that you have to trigger a configuration operation
before you try to use the logger in your webapp, so make sure you do
that. I'm not sure how to trigger an XML configuration, but for a
property-file-based config, you just have to instantiate a new
"PropertyConfigurator" object. You can probably do something similar
for an XML-based configuration.

- -chris

Chris,

Here's how I understand things to work. It appears to work in (admittedly small) sample applications I write.

I've always just placed my log4j.xml file in WEB-INF/classes, at the root of that directory tree. This pretty easy to do with a NetBeans or Maven project. I don't know about Eclipse, since I'm not very familiar with it.

Then I write a servlet context listener to start and stop logging. Normally I don't do much there.

According to the documentation, the sequence for log4j startup is:

1. Check if override has been set (log4j.defaultInitOverride).

If so, you're on your own.

2. look for a systems property log4j.configuration.

According to the documentation, this is preferred. However, since you need different log4j.xml files (one for Tomcat, one for each web app), this isn't going to work very well.

3. Convert the resource to a URL

This will fail obviously if it's empty.

4. If step 3 fails, then search for a log4j.xml or log4j.properties
Basically, this will try first the thread context classloader, and then the classloader that loaded this class. Finally, use the system classloader. So, if you have a log4j.xml file in WEB-INF/classes and one in CATALINA_BASE/lib, your web application should find the one in WEB-INF/classes first.

5. If no suitable log4j.xml (or log4j.properties) file is found, abort

So really, for me, it boils down to this.

1. Place log4j.xml in WEB-INF/classes
2. Write a servlet context listener
   a. contextInitialized - just write something to the log
   b. contextDestroyed - shut down logging if the app started it

You could add additional log4j configuration mechanisms in the contextInitialed method (like change log file names based on host name) if you wish.

. . . . just my two cents.
/mde/

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

Reply via email to