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 16:36:35 GMT
On Tue, 21 Aug 2007 09:58:34 -0600
  "Alec Lebedev" <alec.lebedev@nextpage.com> wrote:
> Jake,
> It looks like InitContextListener class is in log4j 1.3 alpha.

Not sure where you're getting that?  It's in the Log4j-sandbox, which does not 
get built along with log4j.jar (neither 1.2.xx nor 1.3). Oh, hold on.  Are you 
getting confused about instructions to check out the "LOG4J_SANDBOX_ALPHA3" 
tag?  That has absolutely nothing to do with log4j 1.3alpha code.  Notice the 
"SANDBOX" in the tag name.  The code in said tag has zero dependencies on 
Log4j 1.3.   Rather, it depends on Log4j 1.2.xx.

> I am
> running Tomcat 5.5 and using log4j 1.2.13. I am not allowed to run alpha
> releases in production. 

The sandbox is unofficial code.  I tagged it as "LOG4J_SANDBOX_ALPHA3" to give 
it some version and provide a snapshot in time that I could depend upon since 
the sandbox can change with experimental stuff at any time.  It's not really 
"alpha".  It's not really anything but a snapshot of code that works (at least 
it works for me).  Use it if you want.  If you don't want to, so be it.

> It also seems that ServletContextLogAppenderListener and
> ServletContextLogAppender can only be found in 1.3 alpha source tree.

Again, a confusion of the name "LOG4J_SANDBOX_ALPHA3".  log4j-sandbox is not 
part of the regular log4j source tree.

> I ended up writing a LoggingInitServlet and configured it to be loaded
> by Tomcat first by setting <load-on-startup> element value to 0 in
> web.xml. In the init() method I load the default log4j properties from
> log4j.properties on the classpath. Then I override the log file name
> property and reconfigure log4j as follows:
> LogManager.resetConfiguration();
> PropertyConfigurator.configure(props);
> This approach works OK, but I would like to have a cleaner solution. Can
> anyone think of one that would work with log4j 1.2.13?

Yes, InitContextListener works with Log4j 1.2.xxx, as does the 
ServletContextLogAppender.  Check the source out and build it against 


> Thanks.
> P.S. Several links, including InitContextListener, on
> http://wiki.apache.org/logging-log4j/AppContainerLogging are broken.

Those aren't really meant to be links.  The Wiki sees them as special names, 
so it automagically creates links for them to pages which are to be defined 
(for better or for worse).  Just ignore them (or maybe I'll actually write 
content for them someday?).

> -----Original Message-----
>From: Jacob Kjome [mailto:hoju@visi.com] 
> Sent: Monday, August 20, 2007 11:33 PM
> To: Log4J Users List
> Subject: Re: How to configure log4j output file based on the servlet
> context?
> 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?
> >
> See:
> http://wiki.apache.org/logging-log4j/AppContainerLogging
> 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.
> >log4j.appender.R.File=${catalina.home}/logs/${servlet.context}.log?
> >
> >
> 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.
> >
> >Thanks.
> Hope that helps!
> Jake 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
>For additional commands, e-mail: log4j-user-help@logging.apache.org
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
>For additional commands, e-mail: log4j-user-help@logging.apache.org

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

View raw message