0

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.

amphibient
  • 29,770
  • 54
  • 146
  • 240
  • My guess would be that some other dependency you have is pulling in commons-logging. If you're using maven or a similar build tool, you should be able to visualise your dependency tree and see what's including commons-logging. – Steven Bakhtiari Feb 21 '13 at 23:21
  • actually, I am using Ant – amphibient Feb 21 '13 at 23:22

1 Answers1

0

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.

Matthew Smith
  • 1,287
  • 1
  • 9
  • 19
  • unfortunately, when I added `slf4j-jcl-1.7.2.jar`, it still complained about not finding commons – amphibient Feb 21 '13 at 23:35
  • what i had in the classpath was: `slf4j-api-1.7.2.jar` and `slf4j-jcl-1.7.2.jar` – amphibient Feb 21 '13 at 23:37
  • 1
    But do you have `commons-logging-1.1.1.jar` in your classpath? `slf4j-jcl-1.7.2.jar` tells SLF4J to use commons-logging, but if you don't have the actual commons-logging jar in your path, it won't work. – Matthew Smith Feb 21 '13 at 23:42