[slf4j-user] log4j-over-slf4j checks circular dependency too strict

Tom Liu Tom.SH.Liu at MorganStanley.com
Tue Aug 10 06:38:05 CEST 2010


Hi guys,

I think log4j-over-slf4j (as well as jcl bridges) checks circular
dependency too strict.
Even when slf4j-log4j12.jar is visible, it does not necessarily mean
it will be picked up by slf4j-api as the logging backend.

Here is a setup in tomcat 6 demonstrate the issue:
tomcat container lib contains: jcl-over-slf4j.jar, slf4j-api.jar,
slf4j-log4j12.jar, log4j.jar
Webapp WEB-INF/lib contains: log4j.jar, log4j-over-slf4j.jar,
slf4j-api.jar, logback-core.jar, logback-access.jar.
What we want to achieve is that tomcat logging (from commons-logging)
will be routed to log4j and webapp's logging (from log4j) will be
routed to logback.
In theory, there will be no circular dependencies in runtime as
logback is picked up as the logging backend. However, webapp failed to
start because the "potential" circular dependency is detected.

Is it possible to change the circular dependency detection algorithm
to test against the actual binding picked up? I did a hack on
org.apache.log4j.Log4jLoggerFactory which works for me:
class Log4jLoggerFactory {
...

static {

        LoggerFactoryBinder binder = StaticLoggerBinder.getSingleton();
        if ("org.slf4j.impl.Log4jLoggerFactory".equals(binder.getLoggerFactoryClassStr()))
{
            String part1 = "Detected both log4j-over-slf4j.jar AND
slf4j-log4j12.jar on the class path, preempting StackOverflowError.
REPLACED ";
            String part2 = "See also " + LOG4J_DELEGATION_LOOP_URL + "
for more details.";

            Util.reportFailure(part1);
            Util.reportFailure(part2);
            throw new IllegalStateException(part1 + part2);
        }

    }
...

}

Thanks,
Tom


More information about the slf4j-user mailing list