1
votes

First off, I apologize if this question has already been asked. I searched but couldn't find anything specific to my problem.

Here's the scenario - I'm deploying an application, say, MyFirstWebApp in Tomcat 6.0.35. Tomcat has a log4j.properties in it's common loader path which looks like -

log4j.rootLogger=INFO, LOGFILE

log4j.logger.com.test.java=DEBUG, TEST
log4j.additivity.com.test.java=false

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%p] %m%n

# LOGFILE is set to be a File appender using a PatternLayout
log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.LOGFILE.File=/home/myuserid/tomcat.log
log4j.appender.LOGFILE.MaxFileSize=25MB
log4j.appender.LOGFILE.MaxBackupIndex=5
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d %p %t %c - %m%n

# LOGFILE is set to be a File appender using a PatternLayout
log4j.appender.TEST=org.apache.log4j.FileAppender
log4j.appender.TEST.File=/home/myuserid/test.log
log4j.appender.TEST.layout=org.apache.log4j.PatternLayout
log4j.appender.TEST.layout.ConversionPattern=%d %p %t %c - %m%n

Tomcat also has a log4j.jar file in it's lib/ directory.

Now, MyFirstWebApp has a log4j.properties inside it's WEB-INF/classes directory. MyFirstWebApp also has a log4j.jar in it's WEB-INF/lib directory.

MyFirstWebApp log4j.properties looks like this -

log4j.rootCategory=INFO,LOGFILE

# Set the enterprise logger priority to FATAL
log4j.logger.com.test=INFO

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%p] %m%n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.LOGFILE.MaxFileSize=10MB
log4j.appender.LOGFILE.MaxBackupIndex=99
log4j.appender.LOGFILE.File=/home/myuserid/mywebapp.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n

When I start the server and deploy this application, I expected to find 3 log files - tomcat.log, test.log and mywebapp.log (which I did). I also expected to find all logging from "com.test.java" package inside "test.log" but they were found in "mywebapp.log"

What am I doing wrong here? How can I log messages from "com.test.java" without modifying MyFirstWebApp?

1

1 Answers

1
votes

The log4j.properties file is loaded by the classloader. All the classes in your webapp use the webapp's classloader, and thus only see the log4j.properties file that is under WEB-INF/classes. And thus, the logs from com.test.java that are generated by classes of your webapp use the configuration found in WEB-INF/classes/log4j.properties. So they're written to mywebapp.log.

If you want the logs from com.test.java to go to test.log, you'll have to either modify the webapp's log4j.properties (right solution), or to put those classes out of the webapp, directly in tomcat's classpath (but don't do this).