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 Didier Bretin <db...@informactis.com> on 2001/01/05 17:33:06 UTC
use of log4j with servlet
Hello,
I try to use log4j with my servlets applications. In all my servlets of
a webapplication I declare in my first servlet for user access the
BasicConfigurator with:
BasicConfigurator.configure(new FileAppender (new PatternLayout ("%r [%t]
%-5p %c %x - %m\n"), "path/to/my/logfile"));
Sometimes, I go throw this line more than one time and if I do this, I got
the same log message in my log file.
Is there a way to detect if a BasicConfigurator is already configurate ?
Thanks.
--
.------------------------------------------------.
.^. | Didier Bretin, France | dbr@informactis.com |
/V\ |-----------------------| www.informactis.com |
// \\ | `------------------------|
/( )\ | Visit: http://www.multimania.com/cieexcalibur/ |
^^-^^ `------------------------------------------------'
Re: use of log4j with servlet
Posted by Ceki Gulcu <cg...@urbanet.ch>.
At 11:08 08.01.2001 +0100, you wrote:
>On Fri, 5 Jan 2001, Nelson Minar wrote:
>
>| >Is there a way to detect if a BasicConfigurator is already configurate ?
>|
>| As a bunch of folks have said; no, not really.
>
>This stuff bit me too when I started using log4j.
>
>Wouldn't it be possible to have a way to "clear" the configuration, so
>that you could run this clear thing before the configuration?
>
>Reloading in Tomcat (and I guess other servlet containers too) makes it
>difficult to ensure that this ONLY happens once (and of course, it has to
>happen at least once too!)..
Hi Endre,
Calling the BasicConfigurator multiple times is a well known problem that
comes up quite frequently. There are a number of solutions:
1) As Nelson Minar suggested in a previous mail, one can use a static
boolean to perform a check. Something along the lines of:
public class MyServlet .... {
private static boolean log4jInitialized = false;
public void init() {
if (!log4jInitialized) {
XXXConfigurator.configure(...);
log4jInitialized = true;
}
.....
}
This is hardly difficult.
If you are using dynamic loading of servlet classes, then you can move the
check to a class that you know will be only loaded once.
2) When loaded to memory, the Category class will search for the file
log4j.properties in the classpath and configure itself with that file if it
is found. This feature has been improved in log4j 1.0, where the file to
search for can be specified as a URL. If the filename extension is .xml,
then the DOMConfigurator is used to parse the config file. More generally,
you can even specify the configurator class to use on the URL.
Hope this provides a better answer. Regards, Ceki
Re: use of log4j with servlet
Posted by Endre Stølsvik <En...@Stolsvik.com>.
On Fri, 5 Jan 2001, Nelson Minar wrote:
| >Is there a way to detect if a BasicConfigurator is already configurate ?
|
| As a bunch of folks have said; no, not really.
This stuff bit me too when I started using log4j.
Wouldn't it be possible to have a way to "clear" the configuration, so
that you could run this clear thing before the configuration?
Reloading in Tomcat (and I guess other servlet containers too) makes it
difficult to ensure that this ONLY happens once (and of course, it has to
happen at least once too!)..
Endre.
Re: use of log4j with servlet
Posted by Nelson Minar <ne...@monkey.org>.
>Is there a way to detect if a BasicConfigurator is already configurate ?
As a bunch of folks have said; no, not really.
One option is to use a special startup class that your servlet engine
invokes. I don't know if there's a standard way to do this in the
servlet API; some engines will invoke any given class you ask it to,
others will run a specially designated "startup servlet".
Another option is to encapsulate your initialization of log4j in your
own static class. Ie, a class like this:
public class LoggingInitializer extends Object {
private static boolean initialized = false;
public static void initialize() {
if (initialized)
return;
BasicConfigurator.configure(...);
}
}
Then just invoke your initialize method. Not super-efficient, but it
works fine. And you will probably find later that having all
initialization centralized in one class simplifies your life when you
start initializing log4j in a more complex way.