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]

Reply via email to