You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Ken Bowen <kb...@als.com> on 2009/03/20 17:08:46 UTC

Parameterizing log4j.properties/xml files

All,

Apologies if I've missed the answer to this in the Logging HowTo, the  
FAQ, or Andre's collection of the logging discussion.

Env:  TC 6.0.18 + Java 1.5

The issue: I want to have several instances of the same webapp running  
on the same Tomcat as myapp1, myapp2, etc.
That's easy.

I'm going to be using log4j, probably with properties files, but xml  
would be ok too.
I'd like to have log files named mylog.log appear in ${catalina.home}/ 
logs looking like

     myapp1-mylog.log    myapp2-mylog.log    ... etc.

I could hardwire this in separte log4j.properties files for each myapp  
instance, something like this:

log4j.appender.myLogFile.File=${catalina.home}/logs/myapp1-mylog.log
log4j.appender.myLogFile.File=${catalina.home}/logs/myapp2-mylog.log

And using ant to build my war files, I could hide this ugliness, just  
copying such hardwired files into the war being built.
But is there any parameter syntax that would allow me to grab the  
context name and write one log4j.properties with something like this:

log4j.appender.myLogFile.File=${catalina.home}/logs/${context-name}- 
mylog.log      ??

Thanks,
Ken


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Parameterizing log4j.properties/xml files

Posted by Rusty Wright <ru...@gmail.com>.
I was doing something like this with LogBack (successor to log4j more or less), but I'm no longer using it and don't remember how I set it up.  The class starts as follows:

  public final class InitLogback extends HttpServlet {
    @Override
    public void init() {
        final String pathPrefix = getServletContext().getRealPath("/");

And I think pathPrefix then contains the name of the directory in the tomcat webapps folder.

I don't remember how I was configuring this servlet in the web.xml file; possibly as a regular servlet with a load-on-startup value that was lower than the main (Spring) servlet.

I don't claim that this is the best way to accomplish this.

If you're using Spring you may want to look at its

org.springframework.web.util.Log4jWebConfigurer

and

org.springframework.web.util.Log4jConfigListener


Ken Bowen wrote:
> All,
> 
> Apologies if I've missed the answer to this in the Logging HowTo, the 
> FAQ, or Andre's collection of the logging discussion.
> 
> Env:  TC 6.0.18 + Java 1.5
> 
> The issue: I want to have several instances of the same webapp running 
> on the same Tomcat as myapp1, myapp2, etc.
> That's easy.
> 
> I'm going to be using log4j, probably with properties files, but xml 
> would be ok too.
> I'd like to have log files named mylog.log appear in 
> ${catalina.home}/logs looking like
> 
>     myapp1-mylog.log    myapp2-mylog.log    ... etc.
> 
> I could hardwire this in separte log4j.properties files for each myapp 
> instance, something like this:
> 
> log4j.appender.myLogFile.File=${catalina.home}/logs/myapp1-mylog.log
> log4j.appender.myLogFile.File=${catalina.home}/logs/myapp2-mylog.log
> 
> And using ant to build my war files, I could hide this ugliness, just 
> copying such hardwired files into the war being built.
> But is there any parameter syntax that would allow me to grab the 
> context name and write one log4j.properties with something like this:
> 
> log4j.appender.myLogFile.File=${catalina.home}/logs/${context-name}-mylog.log      
> ??
> 
> Thanks,
> Ken
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: [OT] Parameterizing log4j.properties/xml files

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ken,

On 3/20/2009 12:08 PM, Ken Bowen wrote:
> But is there any parameter syntax that would allow me to grab the
> context name and write one log4j.properties with something like this:
> 
> log4j.appender.myLogFile.File=${catalina.home}/logs/${context-name}-mylog.log     

Nothing built-into Tomcat or log4j.

Log4j allows for parameterizing its own log files, but you'd have to set
the context-name /in the properties file/ in order for log4j to pick it up.

My advice: use ant to do string-replacement as the WAR file is built.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAknEAlAACgkQ9CaO5/Lv0PDCdACeLHdUmRq7wMrdph1Xjyhi0giR
YkoAn0S344WMud0h6TmlurVt850Jd3yo
=wePB
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org