简体   繁体   中英

Does slf4j use commons-logging implementation by default?

I have only worked with log4j in the past. Now I am scouting a new project and noticing that it uses slf4j 1.7.2 . I understand it is only an API specification which provides a simplified interface (AKA facade) to various implementations that conform to it, such as java.util.logging, log4j and logback . However, I noticed that commons-logging wasn't mentioned in the list on the API web site, however, its jar was in this app's classpath. When I removed it from the classpath to check whether it was the used implemetation, I confirmed that it indeed was:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

Is commons-logging indeed the default implementation for slf4j? Because their documentation says:

If no binding is found on the class path, then SLF4J will default to a no-operation implementation.

at the above linked page. And even though I have slf4j-nop-1.7.2.jar in the classpath, which I am guessing the default implementation referred to above is, I still get the exception asking for org/apache/commons/logging/LogFactory if the commons is not in the classpath, so I am confused.

A clarification would be appreciated.

You need a combination of the binding jar AND the logging implementation (JCL in this case) on the classpath.

Binding with a logging framework at deployment time

commons-logging is not the default, but if you have slf4j-jcl-1.7.2.jar on your classpath, it will try to use that. So if you therefore remove JCL from your path, it will complain that it can't find the classes.

slf4j-nop-1.7.2.jar is the no-op "SLF4J bindings" jar, not an implementation in itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM