Slf4j: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 26: Line 26:


If the classpath is assembled by hand, log4j must be added to the classpath, otherwise we'll get:
If the classpath is assembled by hand, log4j must be added to the classpath, otherwise we'll get:
<pre>
Failed to instantiate SLF4J LoggerFactory
Reported exception:
java.lang.NoClassDefFoundError: org/apache/log4j/Level
[...]
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
[...]
</pre>


=Troubleshooting=
=Troubleshooting=

Revision as of 04:37, 2 December 2016

Internal

Playground Code

https://github.com/NovaOrdis/playground/tree/master/slf4j

Bindings

SLF4J needs an actual binding to a known logging framework. The binding is executed at runtime by the class org.slf4j.impl.StaticLoggerBinder, which is embedded in biding libraries. If no SLF4J binding library could be found on the class path, you will see 'Failed to load class "org.slf4j.impl.StaticLoggerBinder"' (see below for more details).

If you are building an embedded component such as a library or a framework, it must not declare a dependency on any SLF4J binding, but only on slf4j-api. When a library declares a compile-time dependency on a SLF4J binding, it imposes that binding on the end-user, thus negating SLF4J's purpose.

If you are building a runtime that actually does logging, one (and only one) of slf4j-nop.jar, slf4j-simple.jar, slf4j-log4j12.jar, slf4j-jdk14.jar or logback-classic.jar must be added to the classpath. If you add more than one binding to the classpath, you will see "Class path contains multiple SLF4J bindings" (see below for more details). Note that slf4j-nop.jar provide just null logging.

log4j12 Binding

If you want an application that uses slf4j-api to actually log with log4j, add the "slf4j-log4j12.jar" binding to the classpath.

If the slf4j-log4j12 dependency is introduced with Maven, slf4j-log4j12 will transitively declare a dependency on log4j:

Slf4j-log4j12 Dependencies.png

If the classpath is assembled by hand, log4j must be added to the classpath, otherwise we'll get:

Failed to instantiate SLF4J LoggerFactory
Reported exception:
java.lang.NoClassDefFoundError: org/apache/log4j/Level
[...]
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
[...]

Troubleshooting

No StaticLoggerBinder

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.

SLF4J needs an actual binding to a known logging framework. The above message means that the binding, provided by org.slf4j.impl.StaticLoggerBinder, was not found, which means that no appropriate SLF4J binding could be found on the class path.

To fix, you need to place one (and only one) of slf4j-nop.jar, slf4j-simple.jar, slf4j-log4j12.jar, slf4j-jdk14.jar or logback-classic.jar on the classpath.

If you are packaging an application and you do not care about logging, then placing slf4j-nop.jar on the class path of your application will get rid of this warning message. Note that embedded components such as libraries or frameworks should not declare a dependency on any SLF4J binding but only depend on slf4j-api. When a library declares a compile-time dependency on a SLF4J binding, it imposes that binding on the end-user, thus negating SLF4J's purpose.

Multiple SLF4J Bindings

If the classpath contains two or more JARs with SLF4J bindings, we get this:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/ovidiu/runtime/os-stats-1.0.1-SNAPSHOT-6/lib/slf4j-log4j12-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/ovidiu/runtime/jboss-eap-6.4.6/modules/system/layers/base/org/slf4j/impl/main/slf4j-jboss-logmanager-1.0.3.GA-redhat-1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

The solution is to leave only one.

More details:

http://www.slf4j.org/codes.html#multiple_bindings