Thomas,

Here is some code I was given, and some that I wrote to do a very basic
configuration.  Regarding the class of the Configurator and doing the
configuration, I would recommend looking at the static initializer in the
Category class.  I think it does what you want or will at least point you in
the right direction.  I found it to be very informative when figuring this
stuff out.

-Mark

...
        static {
                // if it appears that log4j is not configured, then set up
                // a default configuration so that at least warning or above
end
                // up in the console.
                if (!isConfigured()) {                  
                        doBaseConfigure();
                }
        }
...
        /**
                This code (from Ceki Gülcü) checks to see if there are any
appenders defined for log4j
                which is the definitive way to tell if log4j is already
initialised
        **/
        private static boolean isConfigured() {
                Enumeration enum = Category.getRoot().getAllAppenders();

                if (!(enum instanceof
org.apache.log4j.helpers.NullEnumeration)) {
                        return true;
                }
                else {
                        Enumeration cats =  Category.getCurrentCategories();
                        while (cats.hasMoreElements()) {
                                Category c = (Category) cats.nextElement();
                                if (!(c.getAllAppenders() instanceof
org.apache.log4j.helpers.NullEnumeration))
                                        return true;
                        }
                }
                return false;
        }
        
        private static void doBaseConfigure() {
                // set up the default configuration
                Layout layout = new PatternLayout("%d{ABSOLUTE} %-5p %c{1}
%x : %m%n");
                Appender appender = new ConsoleAppender(layout);
                Category root = Category.getRoot();
                root.addAppender(appender);
                root.setPriority(Priority.WARN);
                root.warn("Log4j not configured, base configuration
(ConsoleAppender/WARN) applied to root category");
        }

-----Original Message-----
From: Thomas Tuft Muller [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, August 14, 2001 4:08 AM
To: [EMAIL PROTECTED]
Subject: Detect already configured log4j


Hi,

I know this has been asked before, but: How do I proceede to detect that
log4j is already configured?

Also: How do I configure log4j with BasicConfigurator from property settings
(the {log4.configuration} system property must identify an class
implementing Configurator and BasicConfigurator does not(!) )?

--

Thomas








*************************************************************************
Copyright ERA Technology Ltd. 2001. (www.era.co.uk). All rights reserved. 
Confidential. No liability whatsoever is accepted for any loss or damage 
suffered as a result of accessing this message or any attachments.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to