Robert,

Thanks for the code.  I integrated it into my code, and it is working great.
When log4j is not configured, I configure it to root/WARN in the code.

I still feel that when log4j initializes (in the static code in Category),
that if it cannot initialize from the configuration property, it should
fallback to root/WARN and report what it has done.  That way, serious errors
can still be reported without having to provide any special configuration
files.

Thanks again,
-Mark

-----Original Message-----
From: Robert Leftwich [mailto:[EMAIL PROTECTED]]
Sent: Monday, July 23, 2001 4:03 PM
To: LOG4J Users Mailing List
Subject: Re: Default Appender/root?


At 06:39 AM 24/07/2001, Mark Womack wrote:
>I know the documentation says that log4j makes no assumptions about the
>logging environment, and so when it cannot find a valid log4j.configuration
>property setting, it reports the error and does nothing...BUT is there a
way
>where you can have it set up something as a default?  For example, if I do
>not specify any settings, I would like log4j to default to root using the
>stock console appender with the priority set to WARN or above.
>
>Is there a way to do this in the code?  If not, there should be, IMHO.
>
>I would appreciate any guidance anyone can provide on this...

A simple solution would be to check if log4j had already been initialized, 
if not then init it yourself using a default_log4j.properties file which 
you ship in your jar (or have in your classpath), i.e. have something like 
the following in a category class (or your main) :

         static {
                 if ( !isConfigured() ) {
 
PropertyConfigurator.configure("\myapp_default_log4j.properties");
                 }
         }

         // 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;
         }

HTH

Robert


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