logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bill-lo...@carpenter.ORG (WJCarpenter)
Subject Re: How Do I Find Out If log4j Has Been Configured?
Date Wed, 28 Apr 2004 23:21:14 GMT
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.  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
     * 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
     * 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
        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

View raw message