You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Jacob Kjome <ho...@visi.com> on 2007/08/21 07:32:30 UTC

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


RE: How to configure log4j output file based on the servlet context?

Posted by "Wilfong, Paul" <pa...@ngc.com>.
Ok - I can follow through with a bug submission suggesting minor updates
to the two pages I referenced.

Thanks much...


-----Original Message-----
From: Curt Arnold [mailto:carnold@apache.org] 
Sent: Thursday, August 23, 2007 8:37 AM
To: Log4J Users List
Subject: Re: How to configure log4j output file based on the servlet
context?


On Aug 23, 2007, at 8:53 AM, Wilfong, Paul wrote:

> Thanks Jake.
>
> I was just trying to provide a bit of feedback to the "Not sure where 
> you're getting that?" question, as to where I picked up 1.3 and why.
>
> Would it be difficult to add a sentence or two to the two web-pages I 
> referenced, to help new users know about all this?
>


The website is going to be completely overhauled with the log4j
1.2.15 release.  A preview is available at http://svn.apache.org/
repos/asf/logging/site/trunk/docs/index.html.  If there is anything that
can make that clearer, please let us know either on the list or by
filing a bug or better yet a bug with a patch.



---------------------------------------------------------------------
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


Re: How to configure log4j output file based on the servlet context?

Posted by Curt Arnold <ca...@apache.org>.
On Aug 23, 2007, at 8:53 AM, Wilfong, Paul wrote:

> Thanks Jake.
>
> I was just trying to provide a bit of feedback to the "Not sure where
> you're getting that?" question, as to where I picked up 1.3 and why.
>
> Would it be difficult to add a sentence or two to the two web-pages I
> referenced, to help new users know about all this?
>


The website is going to be completely overhauled with the log4j  
1.2.15 release.  A preview is available at http://svn.apache.org/ 
repos/asf/logging/site/trunk/docs/index.html.  If there is anything  
that can make that clearer, please let us know either on the list or  
by filing a bug or better yet a bug with a patch.



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


RE: How to configure log4j output file based on the servlet context?

Posted by "Wilfong, Paul" <pa...@ngc.com>.
Thanks Jake.

I was just trying to provide a bit of feedback to the "Not sure where
you're getting that?" question, as to where I picked up 1.3 and why.

Would it be difficult to add a sentence or two to the two web-pages I
referenced, to help new users know about all this?



-----Original Message-----
From: Jacob Kjome [mailto:hoju@visi.com] 
Sent: Wednesday, August 22, 2007 9:44 PM
To: Log4J Users List
Subject: RE: How to configure log4j output file based on the servlet
context?

At 10:31 AM 8/22/2007, you wrote:
 >On Tue, 21 Aug 2007 9:37 "Jacob Kjome" wrote:
 >> Not sure where you're getting that?
 >
 >I also started using 1.3 because it's on the log4j web-site
>http://logging.apache.org/log4j/docs/documentation.html,
 >and 1.2 didn't have XML stuff necessary to deal with log4cxx
>interaction.
 >
 >It seemed Ok to use because of the current web-site info.
 >
 >The 1.3 distribution is available from the log4j web-site:
 >http://logging.apache.org/site/binindex.html.
 >(I don't know if that's the "log4j-sandbox".)  >

Log4j-1.3 is totally abandoned and use of it should cease immediately,
if not sooner.  Log4j-1.2.15 + log4j-companions should ameliorate
concerns of users who depend on Log4j-1.3 features.

Of course Log4j-1.3 is totally beside the point because, as stated
previously, log4j-1.3 has absolutely nothing whatsoever - zilch, nada,
zip - to do with the log4j-sandbox other than the fact that the current
HEAD of the sandbox has some dependencies on log4j-1.3 code, but that's
only because that was the direction of Log4j a while back and it hasn't
been worked on recently.  There are no official releases of the
log4j-sandbox and never will be.  The code that I can guarantee is
compatible with Log4j-1.2.xx is the code tagged as
"LOG4J_SANDBOX_ALPHA3".  I use it every day at work.  You have to check
it out and compile it yourself if you want to use it.

 >In a 15Aug07 post from Curt Arnold, he wrote "...but you should
consider  >migrating away from log4j 1.3 to log4j 1.2 plus appropriate
companions".
 >This is what I now plan to try doing - though I am still trying to
>figure out the best place to get "companions".
 >
 >Perhaps InitContextListener, ServletContextLogAppenderListener, etc.
are  >available in a "companion"?
 >

Not at the moment, and almost certainly not before Log4j-1.2.15 is
released.  Maybe at some point in the future.  The log4j-companions
provides a nice vehicle to release stuff like this where it would have
been difficult to justify it being in the log4j.jar.


Jake

 >
 >
 >-----Original Message-----
 >From: Jacob Kjome [mailto:hoju@visi.com]
 >Sent: Tuesday, August 21, 2007 9:37 AM
 >To: Log4J Users List
 >Subject: Re: How to configure log4j output file based on the servlet
>context?
 >
 >On Tue, 21 Aug 2007 09:58:34 -0600
 >  "Alec Lebedev" <al...@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
>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
 >
 >
 >---------------------------------------------------------------------
 >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


RE: How to configure log4j output file based on the servlet context?

Posted by Jacob Kjome <ho...@visi.com>.
At 10:31 AM 8/22/2007, you wrote:
 >On Tue, 21 Aug 2007 9:37 "Jacob Kjome" wrote:
 >> Not sure where you're getting that?
 >
 >I also started using 1.3 because it's on the log4j web-site
 >http://logging.apache.org/log4j/docs/documentation.html,
 >and 1.2 didn't have XML stuff necessary to deal with log4cxx
 >interaction.
 >
 >It seemed Ok to use because of the current web-site info.
 >
 >The 1.3 distribution is available from the log4j web-site:
 >http://logging.apache.org/site/binindex.html.
 >(I don't know if that's the "log4j-sandbox".)
 >

Log4j-1.3 is totally abandoned and use of it should cease 
immediately, if not sooner.  Log4j-1.2.15 + log4j-companions should 
ameliorate concerns of users who depend on Log4j-1.3 features.

Of course Log4j-1.3 is totally beside the point because, as stated 
previously, log4j-1.3 has absolutely nothing whatsoever - zilch, 
nada, zip - to do with the log4j-sandbox other than the fact that the 
current HEAD of the sandbox has some dependencies on log4j-1.3 code, 
but that's only because that was the direction of Log4j a while back 
and it hasn't been worked on recently.  There are no official 
releases of the log4j-sandbox and never will be.  The code that I can 
guarantee is compatible with Log4j-1.2.xx is the code tagged as 
"LOG4J_SANDBOX_ALPHA3".  I use it every day at work.  You have to 
check it out and compile it yourself if you want to use it.

 >In a 15Aug07 post from Curt Arnold, he wrote "...but you should consider
 >migrating away from log4j 1.3 to log4j 1.2 plus appropriate companions".
 >This is what I now plan to try doing - though I am still trying to
 >figure out the best place to get "companions".
 >
 >Perhaps InitContextListener, ServletContextLogAppenderListener, etc. are
 >available in a "companion"?
 >

Not at the moment, and almost certainly not before Log4j-1.2.15 is 
released.  Maybe at some point in the future.  The log4j-companions 
provides a nice vehicle to release stuff like this where it would 
have been difficult to justify it being in the log4j.jar.


Jake

 >
 >
 >-----Original Message-----
 >From: Jacob Kjome [mailto:hoju@visi.com]
 >Sent: Tuesday, August 21, 2007 9:37 AM
 >To: Log4J Users List
 >Subject: Re: How to configure log4j output file based on the servlet
 >context?
 >
 >On Tue, 21 Aug 2007 09:58:34 -0600
 >  "Alec Lebedev" <al...@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
 >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
 >
 >
 >---------------------------------------------------------------------
 >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


RE: How to configure log4j output file based on the servlet context?

Posted by "Wilfong, Paul" <pa...@ngc.com>.
On Tue, 21 Aug 2007 9:37 "Jacob Kjome" wrote:
> Not sure where you're getting that?

I also started using 1.3 because it's on the log4j web-site
http://logging.apache.org/log4j/docs/documentation.html,
and 1.2 didn't have XML stuff necessary to deal with log4cxx
interaction.

It seemed Ok to use because of the current web-site info.

The 1.3 distribution is available from the log4j web-site:
http://logging.apache.org/site/binindex.html.
(I don't know if that's the "log4j-sandbox".)

In a 15Aug07 post from Curt Arnold, he wrote "...but you should consider
migrating away from log4j 1.3 to log4j 1.2 plus appropriate companions".
This is what I now plan to try doing - though I am still trying to
figure out the best place to get "companions".

Perhaps InitContextListener, ServletContextLogAppenderListener, etc. are
available in a "companion"?



-----Original Message-----
From: Jacob Kjome [mailto:hoju@visi.com] 
Sent: Tuesday, August 21, 2007 9:37 AM
To: Log4J Users List
Subject: Re: How to configure log4j output file based on the servlet
context?

On Tue, 21 Aug 2007 09:58:34 -0600
  "Alec Lebedev" <al...@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
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


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


Re: How to configure log4j output file based on the servlet context?

Posted by Jacob Kjome <ho...@visi.com>.
On Tue, 21 Aug 2007 09:58:34 -0600
  "Alec Lebedev" <al...@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 
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


RE: How to configure log4j output file based on the servlet context?

Posted by Alec Lebedev <al...@nextpage.com>.
Jake,

It looks like InitContextListener class is in log4j 1.3 alpha. I am
running Tomcat 5.5 and using log4j 1.2.13. I am not allowed to run alpha
releases in production. 

It also seems that ServletContextLogAppenderListener and
ServletContextLogAppender can only be found in 1.3 alpha 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?

Thanks.

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

-----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