logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jacob Kjome <h...@visi.com>
Subject Re: How to configure log4j output file based on the servlet context?
Date Tue, 21 Aug 2007 05:32:30 GMT
At 04:39 PM 8/20/2007, you wrote:
 >I am using log4j in my webapp, which is packaged as webapp.war file. I
 >deploy this web application under multiple contexts in Tomcat by
 >renaming the WAR file and deploying it. For example, I create two copies
 >of the WAR file, e.g. context1.war and context2.war and deploy them.
 >Now, I want log4j to output logs from context1.war and context2.war to
 >different log files, e.g. context1.log and context2.log. I would like
 >this configuration to be done at deployment time automatically so that I
 >don't have to change it manually for each context.
 >One way to do this is to use a log4j Initialization Servlet as explained
 >here: http://logging.apache.org/log4j/docs/manual.html. If I use this
 >approach and inside of the log4j initialization servlet get servlet
 >context, then how do I change the target log4j output file at runtime?
 >Can I use PropertyConfigurator for that?


Specifically, read the javadoc for InitContextListener.java.  You can 
achieve automatic log file location per/webapp.  Though this won't 
quite work simply by renaming the context name since the file path in 
the log4j config file would be related to your context name.  For 
instance, if you have a context called "myapp", then the log 
directory location reference would be named ${myapp.log.home}.  The 
naming needs to be unique per/webapp because a system property is set 
just before configuration (based on the name of the context) by 
InitContextListener.  If the name isn't unique, then it would get 
overwritten all the time.  Not only that, but it needs to be 
predictable so you can actually reference it.  This naming scheme 
makes it predictable.  The javadoc goes over this in detail.

In any case, this might not be exactly what you are looking 
for.  However, read below for a possible solution...

 >Are there any alternative approaches? For example, is there a way for
 >log4j to access servlet context properties, e.g.

Actually, yes.  What you can do is set up a master log4j.properties 
and log4j.jar in Tomcat's common library location ("common/lib" and 
commons/classes for 5.5.xx and "lib" for 6.0.xx).  For Tomcat6, make 
sure to read the logging instructions to get Log4j logging 
enabled.  For Tomcat5.5, make sure to put commons-logging.jar in 
"common/lib".  Read the Tomcat docs for how context loggers are named 
and create a logger definition for it as well as its own FileAppender.

Once you have all that set up, have a look at the javadoc for 
ServletContextLogAppenderListener and ServletContextLogAppender (both 
in the sandbox alongside InitContextListener).   Once you have this 
set up, your logging will go through servletContext.log() and show up 
in the context log file defined at the server level.  This means that 
your config for each app doesn't have to deal with logging locations 
at all.  The only place that deals with log locations is the global 
log4j.properties used for Tomcat logging.


Hope that helps!


To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org

View raw message