Return-Path: Delivered-To: apmail-logging-log4j-user-archive@www.apache.org Received: (qmail 12179 invoked from network); 21 Aug 2007 16:37:12 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 21 Aug 2007 16:37:12 -0000 Received: (qmail 44056 invoked by uid 500); 21 Aug 2007 16:37:04 -0000 Delivered-To: apmail-logging-log4j-user-archive@logging.apache.org Received: (qmail 44033 invoked by uid 500); 21 Aug 2007 16:37:04 -0000 Mailing-List: contact log4j-user-help@logging.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Log4J Users List" Reply-To: "Log4J Users List" Delivered-To: mailing list log4j-user@logging.apache.org Received: (qmail 44010 invoked by uid 99); 21 Aug 2007 16:37:04 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 21 Aug 2007 09:37:04 -0700 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of hoju@visi.com designates 208.42.176.221 as permitted sender) Received: from [208.42.176.221] (HELO g2host.com) (208.42.176.221) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 21 Aug 2007 16:37:02 +0000 Received: from [162.136.192.1] (account hoju@visi.com) by mailback2.g2host.com (CommuniGate Pro WEBUSER 5.1.10) with HTTP id 6481657 for log4j-user@logging.apache.org; Tue, 21 Aug 2007 11:36:35 -0500 From: "Jacob Kjome" Subject: Re: How to configure log4j output file based on the servlet context? To: "Log4J Users List" X-Mailer: CommuniGate Pro WebUser v5.1.10 Date: Tue, 21 Aug 2007 11:36:35 -0500 Message-ID: In-Reply-To: References: <20070821053254.35288724B25@athena.apache.org> MIME-Version: 1.0 Content-Type: text/plain;charset=iso-8859-1;format="flowed" Content-Transfer-Encoding: 8bit X-Virus-Checked: Checked by ClamAV on apache.org On Tue, 21 Aug 2007 09:58:34 -0600 "Alec Lebedev" 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 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 log4j-1.2.xx. Jake > 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