logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jacob Kjome <h...@visi.com>
Subject Re: How Do I Find Out If log4j Has Been Configured?
Date Thu, 29 Apr 2004 04:07:15 GMT
At 04:21 PM 4/28/2004 -0700, you wrote:
>jk> There are ways to figure out if Log4j has been configured in
>jk> 1.2.x.  Try this...
>
>jk> http://wiki.apache.org/logging-log4j/Log4JProjectPages/UsefulCode
>
>I'm a little confused by this cite.  I presume you're talking about
>the Log4jConfigurator example.

Did you look at the code above the Log4jConfigurator example?   Looks 
pretty similar to what you are doing below.  BTW, if you think your example 
is better or otherwise useful, why not add it to the wiki?

Jake


>  That looks like a fairly vanilla
>example of a singleton class to keep yourself from double-dipping
>log4j.  That does nothing to tell you if it's already been configured
>by somebody else, or am I misreading it?
>
>We do this around here:
>
>     /**
>      * log4j has a wacky default if it doesn't get any configuration help 
> from the environment.
>      * It turns on DEBUG at the RootLogger level.  So, everything wrapped 
> in isDebugEnabled will
>      * be done even though it is written to nowhere.  Grrr.  This method 
> looks for that condition.
>      * If all loggers (1) have no appenders, and (2) are at DEBUG level, 
> then fix things up by
>      * setting the RootLogger at the ERROR level (which will cascade down).
>      * @return whether or not corrective action was taken.
>      */
>     protected static boolean checkForCrazyLog4jDefault()
>     {
>         org.apache.log4j.Logger rl = org.apache.log4j.Logger.getRootLogger();
>         if (!loggerIsThatCrazyWay(rl)) return false;
>         Enumeration allLoggers = LogManager.getCurrentLoggers();
>         while (allLoggers.hasMoreElements())
>         {
>             org.apache.log4j.Logger lgr = 
> (org.apache.log4j.Logger)allLoggers.nextElement();
>             if (! loggerIsThatCrazyWay(lgr)) return false;
>         }
>         // OK, I guess it *is* that crazy way... change to a different, 
> slightly less crazy way
>         rl.setLevel(Level.ERROR);
>         return true;
>     }
>     private static boolean loggerIsThatCrazyWay(org.apache.log4j.Logger lgr)
>     {
>         Enumeration appenders = lgr.getAllAppenders();
>         if (appenders.hasMoreElements()) return false;
>         Level lvl = lgr.getEffectiveLevel();
>         if (lvl != null)
>         {
>             if (! lvl.equals(Level.DEBUG)) return false;
>         }
>         return true;
>     }
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
>For additional commands, e-mail: log4j-user-help@logging.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message