You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Simon <sp...@gmail.com> on 2011/12/19 17:54:30 UTC

Logging per webapp per instance

Hi,

Our webapp is packaged with the log4j framework (1.2.16) and a log4j
configuration that redirect all logs to a file on the server
filesystem. This log file is rotated when it reaches 50M.

Here is the configuration detail :

log4j.rootLogger=INFO, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/var/log/webapp.log
log4j.appender.file.MaxFileSize=50000KB
log4j.appender.file.MaxBackupIndex=50
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %-5p %c - %m%n

This configuration file is located inside the webapp.war archive at
/WEB-INF/classes/log4j.properties.

In our production environment, we started with one tomcat instance and
everything was fine. The tomcat version is 5.5.20 and it is running on
the Oracle JVM 1.6.0_23-b05.


Unfortunately, when the infrastructure team added 2 other Tomcat
instances on the same server with the same webapp.war inside, some
logs were lost. The 2 instances are basically a complete copy of the
file tree of the first instance. Thus, there is 3 identical webapps
logging into the same log file simultaneously :
/tomcat/instance-1/webapps/webapp.war => /var/log/webapp.log
/tomcat/instance-2/webapps/webapp.war => /var/log/webapp.log
/tomcat/instance-3/webapps/webapp.war => /var/log/webapp.log

My hypothesis is that as every webapp log to the same log file from a
different JVM (the tomcat instances), there could be some concurrency
issues when the rolling from log4j occurs !!!


I would like that each webapp logs to a particular file without having
a different log4j configuration packaged in the webapp.war archive. My
first idea was to modify the log4j configuration so the webapp will
log to the console (stdout) and then find a way to redirect the stdout
inside a Tomcat instance to a particular file :
/tomcat/instance-1/webapps/webapp.war => stdout => /var/log/webapp-1.log
/tomcat/instance-2/webapps/webapp.war => stdout => /var/log/webapp-2.log
/tomcat/instance-3/webapps/webapp.war => stdout => /var/log/webapp-3.log

I know i could achieve that with the Resin application server through
the use of the <stdout /> configuration element
(http://caucho.com/resin-4.0/reference.xtp#stdoutlog,
http://caucho.com/resin-4.0/admin/logging.xtp).

Is there a way to mimic this behaviour in Tomcat ?

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


Re: Logging per webapp per instance

Posted by Simon <sp...@gmail.com>.
Hi,

I will use the variable substitution solution by changing the
log4j.properties file :

log4j.logFileSuffix=...
log4j.appender.file.File=/var/log/webapp-${logSuffix}.log

The value will be overriden by each instances with
-Dlog4j.logFileSuffix=whatever

Thank you for your help !

Regards,
Simon

2011/12/19 Daniel Mikusa <dm...@vmware.com>:
> On Mon, 2011-12-19 at 09:05 -0800, Leon Rosenberg wrote:
>> Hello,
>>
>> you can specify the logoutput as relative path:
>> instead of
>> log4j.appender.file.File=/var/log/webapp.log
>> to
>> log4j.appender.file.File=logs/webapp.log
>> and link the local file to the desired destination.
>>
>
> +1
>
> or if you don't want to use relative paths, you could use a variable
> substitution.
>
> Ex:
>
>  log4j.appender.file.File=${catalina.base}/logs/webapp.log
>
>
> Property substitution is not limited to "catalina.base" (it's just easy
> to use since it's defined automatically), Log4J can substitute any
> system property.  So you could define your own.
>
> Ex:
>
> In bin/setenv.sh...
>
>  -Dmy.log.prefix=server1
>
> In log4j.properties...
>
>  log4j.appender.file.File=/var/log/${my.log.prefix}-webapp.log
>
>
> For more details on property substitution.
>
> https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html
>
>
> Dan
>
>
>
>> regards
>> Leon
>>
>> On Mon, Dec 19, 2011 at 5:54 PM, Simon <sp...@gmail.com> wrote:
>> > Hi,
>> >
>> > Our webapp is packaged with the log4j framework (1.2.16) and a log4j
>> > configuration that redirect all logs to a file on the server
>> > filesystem. This log file is rotated when it reaches 50M.
>> >
>> > Here is the configuration detail :
>> >
>> > log4j.rootLogger=INFO, file
>> > log4j.appender.file=org.apache.log4j.RollingFileAppender
>> > log4j.appender.file.File=/var/log/webapp.log
>> > log4j.appender.file.MaxFileSize=50000KB
>> > log4j.appender.file.MaxBackupIndex=50
>> > log4j.appender.file.layout=org.apache.log4j.PatternLayout
>> > log4j.appender.file.layout.ConversionPattern=%d %-5p %c - %m%n
>> >
>> > This configuration file is located inside the webapp.war archive at
>> > /WEB-INF/classes/log4j.properties.
>> >
>> > In our production environment, we started with one tomcat instance and
>> > everything was fine. The tomcat version is 5.5.20 and it is running on
>> > the Oracle JVM 1.6.0_23-b05.
>> >
>> >
>> > Unfortunately, when the infrastructure team added 2 other Tomcat
>> > instances on the same server with the same webapp.war inside, some
>> > logs were lost. The 2 instances are basically a complete copy of the
>> > file tree of the first instance. Thus, there is 3 identical webapps
>> > logging into the same log file simultaneously :
>> > /tomcat/instance-1/webapps/webapp.war => /var/log/webapp.log
>> > /tomcat/instance-2/webapps/webapp.war => /var/log/webapp.log
>> > /tomcat/instance-3/webapps/webapp.war => /var/log/webapp.log
>> >
>> > My hypothesis is that as every webapp log to the same log file from a
>> > different JVM (the tomcat instances), there could be some concurrency
>> > issues when the rolling from log4j occurs !!!
>> >
>> >
>> > I would like that each webapp logs to a particular file without having
>> > a different log4j configuration packaged in the webapp.war archive. My
>> > first idea was to modify the log4j configuration so the webapp will
>> > log to the console (stdout) and then find a way to redirect the stdout
>> > inside a Tomcat instance to a particular file :
>> > /tomcat/instance-1/webapps/webapp.war => stdout => /var/log/webapp-1.log
>> > /tomcat/instance-2/webapps/webapp.war => stdout => /var/log/webapp-2.log
>> > /tomcat/instance-3/webapps/webapp.war => stdout => /var/log/webapp-3.log
>> >
>> > I know i could achieve that with the Resin application server through
>> > the use of the <stdout /> configuration element
>> > (http://caucho.com/resin-4.0/reference.xtp#stdoutlog,
>> > http://caucho.com/resin-4.0/admin/logging.xtp).
>> >
>> > Is there a way to mimic this behaviour in Tomcat ?
>> >
>> > ---------------------------------------------------------------------
>> > 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
>>

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


Re: Logging per webapp per instance

Posted by Daniel Mikusa <dm...@vmware.com>.
On Mon, 2011-12-19 at 09:05 -0800, Leon Rosenberg wrote:
> Hello,
> 
> you can specify the logoutput as relative path:
> instead of
> log4j.appender.file.File=/var/log/webapp.log
> to
> log4j.appender.file.File=logs/webapp.log
> and link the local file to the desired destination.
> 

+1

or if you don't want to use relative paths, you could use a variable
substitution.

Ex:

  log4j.appender.file.File=${catalina.base}/logs/webapp.log


Property substitution is not limited to "catalina.base" (it's just easy
to use since it's defined automatically), Log4J can substitute any
system property.  So you could define your own.

Ex:

In bin/setenv.sh...

  -Dmy.log.prefix=server1

In log4j.properties...

  log4j.appender.file.File=/var/log/${my.log.prefix}-webapp.log


For more details on property substitution.

https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html


Dan



> regards
> Leon
> 
> On Mon, Dec 19, 2011 at 5:54 PM, Simon <sp...@gmail.com> wrote:
> > Hi,
> >
> > Our webapp is packaged with the log4j framework (1.2.16) and a log4j
> > configuration that redirect all logs to a file on the server
> > filesystem. This log file is rotated when it reaches 50M.
> >
> > Here is the configuration detail :
> >
> > log4j.rootLogger=INFO, file
> > log4j.appender.file=org.apache.log4j.RollingFileAppender
> > log4j.appender.file.File=/var/log/webapp.log
> > log4j.appender.file.MaxFileSize=50000KB
> > log4j.appender.file.MaxBackupIndex=50
> > log4j.appender.file.layout=org.apache.log4j.PatternLayout
> > log4j.appender.file.layout.ConversionPattern=%d %-5p %c - %m%n
> >
> > This configuration file is located inside the webapp.war archive at
> > /WEB-INF/classes/log4j.properties.
> >
> > In our production environment, we started with one tomcat instance and
> > everything was fine. The tomcat version is 5.5.20 and it is running on
> > the Oracle JVM 1.6.0_23-b05.
> >
> >
> > Unfortunately, when the infrastructure team added 2 other Tomcat
> > instances on the same server with the same webapp.war inside, some
> > logs were lost. The 2 instances are basically a complete copy of the
> > file tree of the first instance. Thus, there is 3 identical webapps
> > logging into the same log file simultaneously :
> > /tomcat/instance-1/webapps/webapp.war => /var/log/webapp.log
> > /tomcat/instance-2/webapps/webapp.war => /var/log/webapp.log
> > /tomcat/instance-3/webapps/webapp.war => /var/log/webapp.log
> >
> > My hypothesis is that as every webapp log to the same log file from a
> > different JVM (the tomcat instances), there could be some concurrency
> > issues when the rolling from log4j occurs !!!
> >
> >
> > I would like that each webapp logs to a particular file without having
> > a different log4j configuration packaged in the webapp.war archive. My
> > first idea was to modify the log4j configuration so the webapp will
> > log to the console (stdout) and then find a way to redirect the stdout
> > inside a Tomcat instance to a particular file :
> > /tomcat/instance-1/webapps/webapp.war => stdout => /var/log/webapp-1.log
> > /tomcat/instance-2/webapps/webapp.war => stdout => /var/log/webapp-2.log
> > /tomcat/instance-3/webapps/webapp.war => stdout => /var/log/webapp-3.log
> >
> > I know i could achieve that with the Resin application server through
> > the use of the <stdout /> configuration element
> > (http://caucho.com/resin-4.0/reference.xtp#stdoutlog,
> > http://caucho.com/resin-4.0/admin/logging.xtp).
> >
> > Is there a way to mimic this behaviour in Tomcat ?
> >
> > ---------------------------------------------------------------------
> > 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: Logging per webapp per instance

Posted by Leon Rosenberg <ro...@gmail.com>.
Hello,

you can specify the logoutput as relative path:
instead of
log4j.appender.file.File=/var/log/webapp.log
to
log4j.appender.file.File=logs/webapp.log
and link the local file to the desired destination.

regards
Leon

On Mon, Dec 19, 2011 at 5:54 PM, Simon <sp...@gmail.com> wrote:
> Hi,
>
> Our webapp is packaged with the log4j framework (1.2.16) and a log4j
> configuration that redirect all logs to a file on the server
> filesystem. This log file is rotated when it reaches 50M.
>
> Here is the configuration detail :
>
> log4j.rootLogger=INFO, file
> log4j.appender.file=org.apache.log4j.RollingFileAppender
> log4j.appender.file.File=/var/log/webapp.log
> log4j.appender.file.MaxFileSize=50000KB
> log4j.appender.file.MaxBackupIndex=50
> log4j.appender.file.layout=org.apache.log4j.PatternLayout
> log4j.appender.file.layout.ConversionPattern=%d %-5p %c - %m%n
>
> This configuration file is located inside the webapp.war archive at
> /WEB-INF/classes/log4j.properties.
>
> In our production environment, we started with one tomcat instance and
> everything was fine. The tomcat version is 5.5.20 and it is running on
> the Oracle JVM 1.6.0_23-b05.
>
>
> Unfortunately, when the infrastructure team added 2 other Tomcat
> instances on the same server with the same webapp.war inside, some
> logs were lost. The 2 instances are basically a complete copy of the
> file tree of the first instance. Thus, there is 3 identical webapps
> logging into the same log file simultaneously :
> /tomcat/instance-1/webapps/webapp.war => /var/log/webapp.log
> /tomcat/instance-2/webapps/webapp.war => /var/log/webapp.log
> /tomcat/instance-3/webapps/webapp.war => /var/log/webapp.log
>
> My hypothesis is that as every webapp log to the same log file from a
> different JVM (the tomcat instances), there could be some concurrency
> issues when the rolling from log4j occurs !!!
>
>
> I would like that each webapp logs to a particular file without having
> a different log4j configuration packaged in the webapp.war archive. My
> first idea was to modify the log4j configuration so the webapp will
> log to the console (stdout) and then find a way to redirect the stdout
> inside a Tomcat instance to a particular file :
> /tomcat/instance-1/webapps/webapp.war => stdout => /var/log/webapp-1.log
> /tomcat/instance-2/webapps/webapp.war => stdout => /var/log/webapp-2.log
> /tomcat/instance-3/webapps/webapp.war => stdout => /var/log/webapp-3.log
>
> I know i could achieve that with the Resin application server through
> the use of the <stdout /> configuration element
> (http://caucho.com/resin-4.0/reference.xtp#stdoutlog,
> http://caucho.com/resin-4.0/admin/logging.xtp).
>
> Is there a way to mimic this behaviour in Tomcat ?
>
> ---------------------------------------------------------------------
> 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: Logging per webapp per instance

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

Simon,

On 12/21/11 2:55 AM, Simon wrote:
> It is not really a hack in log4j configuration to redirect the logs
> in stdout, as it is exactly what does the ConsoleAppender by
> default...

True, log4j supports log-to-console. It's only a hack because of the
other stuff you have to do to get it to work.

> The idea here is that we (the developpers) can focus on *what* get 
> logged by configuring the log categories and the log pattern in
> the log4.properties file using a simple ConsoleAppender.

I understand the use case for a logging framework.

> Our infrastructure team (responsible for the server configuration
> and resources) can focus on *where* the logs go (a file, a rolling
> file, a JMS queue, syslog, or whatever the need) in the Tomcat
> configuration.
> 
> This has the other advantage to have a log4.configuration file and
> a web application that are not too much dependant of the
> environment (Unix file path vs Windows file path for example)...

So, you're saying that you want your intra team to be able to dictate
where the logs go, but you don't want them to be able to configure the
logger properly to do that. I'm not a huge fan of that philosophy.

> I guess this why the <stdout /> feature is available in Resin...
> At least it would help in this use case !

As I said, you can do this in Tomcat. I just think it's a bad idea.
The "swallowOutput" flag IMO is a hack to get around applications that
are still using System.out for logging purposes. What you are trying
to do is take a perfectly reasonably use of a logging system and turn
back the clock to 1997. If that's what works for you, go for it. I
just think there are better solutions.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk7x+3wACgkQ9CaO5/Lv0PAY5ACdH9CbpvjSVmWANTNZmYhYNu7n
xQUAoK23Aj5Vd52qL2/Tungyp665SXzc
=+cRq
-----END PGP SIGNATURE-----

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


Re: Logging per webapp per instance

Posted by Simon <sp...@gmail.com>.
2011/12/21 Konstantin Kolinko <kn...@gmail.com>:
> 2011/12/20 Christopher Schultz <ch...@christopherschultz.net>:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Simon,
>>
>> On 12/19/11 11:54 AM, Simon wrote:
>>> log4j.appender.file.File=/var/log/webapp.log
>>>
>>> Thus, there is 3 identical webapps logging into the same log file
>>> simultaneously :
>>
>> It's more likely that the last one wins: the first 2 logs was probably
>> lost entirely.
>
> FileAppender docs say that Append=true by default. Though I wonder
> what will happen during log rotation.
>
> Anyway entries from three servers in the same log file where they
> cannot be distinguished from each other are of little help.
>
>>
>>> My hypothesis is that as every webapp log to the same log file from
>>> a different JVM (the tomcat instances), there could be some
>>> concurrency issues when the rolling from log4j occurs !!!
>>
>> "Some" issues definitely. You need to change this.
>>
>>> I would like that each webapp logs to a particular file without
>>> having a different log4j configuration packaged in the webapp.war
>>> archive. My first idea was to modify the log4j configuration so the
>>> webapp will log to the console (stdout) and then find a way to
>>> redirect the stdout inside a Tomcat instance to a particular file
>>
>> That sounds awful: a hack (in log4j config) to go to stdout and then
>> another hack (in Tomcat config) to redirect to a file. Yuck.
>>

It is not really a hack in log4j configuration to redirect the logs in
stdout, as it is exactly what does the ConsoleAppender by default...

The idea here is that we (the developpers) can focus on *what* get
logged by configuring the log categories and the log pattern in the
log4.properties file using a simple ConsoleAppender.

Our infrastructure team (responsible for the server configuration and
resources) can focus on *where* the logs go (a file, a rolling file, a
JMS queue, syslog, or whatever the need) in the Tomcat configuration.

This has the other advantage to have a log4.configuration file and a
web application that are not too much dependant of the environment
(Unix file path vs Windows file path for example)...

I guess this why the <stdout /> feature is available in Resin... At
least it would help in this use case !


>>> Is there a way to mimic this behaviour in Tomcat ?
>>
>> *Sigh* if you have to, you can use the "swallowOutput" attribute on
>> your <Context> definition. Then you have to configure Tomcat's logging
>> system to put the logs where you want them.
>
> That is fragile. It depends on who catches the value of System.out
> first, Tomcat or a logging framework.
>
>>
>> IMO a better idea would be to configure log4j properly. I think you
>> have several options:
>>
>> 1. Modify the log4j.properties file in each WAR file to specify
>>   the proper filename. You said you didn't want to do this.
>>
>> 2. Use a log4j.properties file that is outside your WAR file.
>>   You'll have to figure out how to get your webapp to detect
>>   the right file so that your 3 separate copies don't end up
>>   reading the same file and reproducing the same issue described
>>   above.
>>
>> 3. In your log4j startup code, set a property based upon some
>>   configuration or environment (context path?) that can be used
>>   by log4j during configuration and setup of the logging system.
>>   I personally favor this approach. If you are running in separate
>>   JVMs, I think you can use system properties and have those
>>   replaced by log4j when parsing the configuration file(s).
>>   Read the documentation to be sure.
>>
>> 4. Configure log4j to log to syslog, and have syslog take care of
>>   the file situation. Of course, you'll have to identify each
>>   separate instance of your webapp if you want to be able to
>>   sort-out which log came from which source. So, you still
>>   have to somehow identify these webapps uniquely.
>>
>
> 5. Configure it to write ${catalina.base}/logs.
>
> (That is if all webapps are on different Tomcat instances, as opposed
> to having several different-named webapps on the same Tomcat)
>
> If you want that to be written to /var/logs, you can replace
> ${catalina.base}/logs with a symlink to /var/logs/instance-N/.
>
> Best regards,
> Konstantin Kolinko
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>

Thank you for your ideas and advices, it will definitely help me a lot !

Regards,

Simon

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


Re: Logging per webapp per instance

Posted by Konstantin Kolinko <kn...@gmail.com>.
2011/12/20 Christopher Schultz <ch...@christopherschultz.net>:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Simon,
>
> On 12/19/11 11:54 AM, Simon wrote:
>> log4j.appender.file.File=/var/log/webapp.log
>>
>> Thus, there is 3 identical webapps logging into the same log file
>> simultaneously :
>
> It's more likely that the last one wins: the first 2 logs was probably
> lost entirely.

FileAppender docs say that Append=true by default. Though I wonder
what will happen during log rotation.

Anyway entries from three servers in the same log file where they
cannot be distinguished from each other are of little help.

>
>> My hypothesis is that as every webapp log to the same log file from
>> a different JVM (the tomcat instances), there could be some
>> concurrency issues when the rolling from log4j occurs !!!
>
> "Some" issues definitely. You need to change this.
>
>> I would like that each webapp logs to a particular file without
>> having a different log4j configuration packaged in the webapp.war
>> archive. My first idea was to modify the log4j configuration so the
>> webapp will log to the console (stdout) and then find a way to
>> redirect the stdout inside a Tomcat instance to a particular file
>
> That sounds awful: a hack (in log4j config) to go to stdout and then
> another hack (in Tomcat config) to redirect to a file. Yuck.
>
>> Is there a way to mimic this behaviour in Tomcat ?
>
> *Sigh* if you have to, you can use the "swallowOutput" attribute on
> your <Context> definition. Then you have to configure Tomcat's logging
> system to put the logs where you want them.

That is fragile. It depends on who catches the value of System.out
first, Tomcat or a logging framework.

>
> IMO a better idea would be to configure log4j properly. I think you
> have several options:
>
> 1. Modify the log4j.properties file in each WAR file to specify
>   the proper filename. You said you didn't want to do this.
>
> 2. Use a log4j.properties file that is outside your WAR file.
>   You'll have to figure out how to get your webapp to detect
>   the right file so that your 3 separate copies don't end up
>   reading the same file and reproducing the same issue described
>   above.
>
> 3. In your log4j startup code, set a property based upon some
>   configuration or environment (context path?) that can be used
>   by log4j during configuration and setup of the logging system.
>   I personally favor this approach. If you are running in separate
>   JVMs, I think you can use system properties and have those
>   replaced by log4j when parsing the configuration file(s).
>   Read the documentation to be sure.
>
> 4. Configure log4j to log to syslog, and have syslog take care of
>   the file situation. Of course, you'll have to identify each
>   separate instance of your webapp if you want to be able to
>   sort-out which log came from which source. So, you still
>   have to somehow identify these webapps uniquely.
>

5. Configure it to write ${catalina.base}/logs.

(That is if all webapps are on different Tomcat instances, as opposed
to having several different-named webapps on the same Tomcat)

If you want that to be written to /var/logs, you can replace
${catalina.base}/logs with a symlink to /var/logs/instance-N/.

Best regards,
Konstantin Kolinko

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


Re: Logging per webapp per instance

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

Simon,

On 12/19/11 11:54 AM, Simon wrote:
> log4j.appender.file.File=/var/log/webapp.log
> 
> Thus, there is 3 identical webapps logging into the same log file 
> simultaneously :

It's more likely that the last one wins: the first 2 logs was probably
lost entirely.

> My hypothesis is that as every webapp log to the same log file from
> a different JVM (the tomcat instances), there could be some
> concurrency issues when the rolling from log4j occurs !!!

"Some" issues definitely. You need to change this.

> I would like that each webapp logs to a particular file without
> having a different log4j configuration packaged in the webapp.war
> archive. My first idea was to modify the log4j configuration so the
> webapp will log to the console (stdout) and then find a way to
> redirect the stdout inside a Tomcat instance to a particular file

That sounds awful: a hack (in log4j config) to go to stdout and then
another hack (in Tomcat config) to redirect to a file. Yuck.

> Is there a way to mimic this behaviour in Tomcat ?

*Sigh* if you have to, you can use the "swallowOutput" attribute on
your <Context> definition. Then you have to configure Tomcat's logging
system to put the logs where you want them.

IMO a better idea would be to configure log4j properly. I think you
have several options:

1. Modify the log4j.properties file in each WAR file to specify
   the proper filename. You said you didn't want to do this.

2. Use a log4j.properties file that is outside your WAR file.
   You'll have to figure out how to get your webapp to detect
   the right file so that your 3 separate copies don't end up
   reading the same file and reproducing the same issue described
   above.

3. In your log4j startup code, set a property based upon some
   configuration or environment (context path?) that can be used
   by log4j during configuration and setup of the logging system.
   I personally favor this approach. If you are running in separate
   JVMs, I think you can use system properties and have those
   replaced by log4j when parsing the configuration file(s).
   Read the documentation to be sure.

4. Configure log4j to log to syslog, and have syslog take care of
   the file situation. Of course, you'll have to identify each
   separate instance of your webapp if you want to be able to
   sort-out which log came from which source. So, you still
   have to somehow identify these webapps uniquely.

Hope that helps,
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk7w0ZIACgkQ9CaO5/Lv0PAnlgCgmSXshaoYgwz1XRd+bl9JJZ2a
dF0An15eYuAzMy5w8A2AwZN4FEF229/A
=WE6F
-----END PGP SIGNATURE-----

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