I have a Tomcat 6 server with a lot of web application directories and I have set up Log4J. It works for all application directories which are not deployed via a war file.
The current configuration looks like this:
/tomcat/lib/log4j.properties
# Define all the appenders
log4j.appender.root=org.apache.log4j.RollingFileAppender
log4j.appender.root.File=${catalina.base}/logs/tomcat.log
log4j.appender.root.Append=true
log4j.appender.root.MaxFileSize=1MB
log4j.appender.root.MaxBackupIndex=5
log4j.appender.root.Encoding=UTF-8
log4j.appender.root.layout=org.apache.log4j.PatternLayout
log4j.appender.root.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
# Configure which loggers log to which appenders
log4j.rootLogger=INFO, root
And in every single web application:
/content/pages/WEB-INF/classes/log4j.properties
log4j.appender.root=org.apache.log4j.RollingFileAppender
log4j.appender.root.File=${catalina.base}/logs/pages/root.log
log4j.appender.root.Append=true
log4j.appender.root.MaxFileSize=1MB
log4j.appender.root.MaxBackupIndex=5
log4j.appender.root.Encoding=UTF-8
log4j.appender.root.layout=org.apache.log4j.PatternLayout
log4j.appender.root.layout.ConversionPattern=%-5p %d (%c)%m%n
log4j.appender.pages=org.apache.log4j.RollingFileAppender
log4j.appender.pages.File=${catalina.base}/logs/pages/pages.log
log4j.appender.pages.Append=true
log4j.appender.pages.MaxFileSize=1MB
log4j.appender.pages.MaxBackupIndex=5
log4j.appender.pages.Encoding=UTF-8
log4j.appender.pages.layout=org.apache.log4j.PatternLayout
log4j.appender.pages.layout.ConversionPattern=%d [%t] %-5p %c- %m%n
log4j.rootLogger=WARN, root
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[content].[/pages]=WARN, pages
Now the one that doesn't work: I have a file "homepageDev.war" inside my /content directory, that gets deployed every time I restart my Tomcat server, or if I trigger it manually.
/content/homepageDev/WEB-INF/classes/log4j.properties
log4j.appender.root=org.apache.log4j.RollingFileAppender
log4j.appender.root.File=${catalina.base}/logs/homepage_dev/root.log
log4j.appender.root.Append=true
log4j.appender.root.MaxFileSize=1MB
log4j.appender.root.MaxBackupIndex=5
log4j.appender.root.Encoding=UTF-8
log4j.appender.root.layout=org.apache.log4j.PatternLayout
log4j.appender.root.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.homepage=org.apache.log4j.RollingFileAppender
log4j.appender.homepage.File=${catalina.base}/logs/homepage_dev/homepage_dev.log
log4j.appender.homepage.Append=true
log4j.appender.homepage.MaxFileSize=1MB
log4j.appender.homepage.MaxBackupIndex=5
log4j.appender.homepage.Encoding=UTF-8
log4j.appender.homepage.layout=org.apache.log4j.PatternLayout
log4j.appender.homepage.layout.ConversionPattern=%-5p %d (%c)%m%n
log4j.rootLogger=DEBUG, root
log4j.logger.com.mycompany.web.homepage=DEBUG, homepage
I have already set up Tomcat to start with Log4J in debug mode and I get a lot of debug information for the all the other applications (e.g. /pages). But in case of the homepageDev.war application I don't get anything.
The only thing that is in the logs is:
[/manager]- Manager: deploy: Deploying web application at '/homepageDev'
[/manager]- Manager: Uploading WAR file to /content/homepageDev.war
Deploying web application archive homepageDev.war
Inside my application I'm using Log4J as the following: I have servlets with a:
private static Logger logger = Logger.getLogger(DatabaseJsonServlet.class);
and in the init method I call:
logger.debug();
Log4j will only initialize itself once, using the thread's current class loader to load
log4j.properties
. So if app A loads first, then log4j will read app A'slog4j.properties
file and then never read anotherlog4j.properties
file ever again.If you want to be able to use log4j separately in each application, then you need to put
log4j-1.2.16.jar
inWEB-INF/lib
for each web application.