Slf4j: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 45: Line 45:


=API=
=API=
=Exception Logging=
<syntaxhighlight >
2018-11-09 13:17:52.947 ERROR 9064 --- [          main] p.s.l.SpringbootLoggingApplication      : failure
java.lang.IllegalStateException: we are in an illegal state
at playground.springboot.logging.subpack.SomeClass.run(SomeClass.java:12) ~[classes!/:na]
at playground.springboot.logging.SpringbootLoggingApplication.run(SpringbootLoggingApplication.java:25) [classes!/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) [spring-boot-2.1.0.RELEASE.jar!/:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) [spring-boot-2.1.0.RELEASE.jar!/:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) [spring-boot-2.1.0.RELEASE.jar!/:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.0.RELEASE.jar!/:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.0.RELEASE.jar!/:2.1.0.RELEASE]
at playground.springboot.logging.SpringbootLoggingApplication.main(SpringbootLoggingApplication.java:15) [classes!/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [springboot-logging-0.0.1-SNAPSHOT.jar:na]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [springboot-logging-0.0.1-SNAPSHOT.jar:na]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [springboot-logging-0.0.1-SNAPSHOT.jar:na]
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [springboot-logging-0.0.1-SNAPSHOT.jar:na]


==Format==
==Format==
Line 51: Line 75:
log.info("accessed by {} on {}", user, date);
log.info("accessed by {} on {}", user, date);
</syntaxhighlight>
</syntaxhighlight>


=Troubleshooting=
=Troubleshooting=

Revision as of 21:18, 9 November 2018

Internal

Playground Code

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

Dependency

dependencies {
    compileOnly('org.slf4j:slf4j-api:1.7.6')
}

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
[...]

API

Exception Logging

2018-11-09 13:17:52.947 ERROR 9064 --- [           main] p.s.l.SpringbootLoggingApplication       : failure

java.lang.IllegalStateException: we are in an illegal state
	at playground.springboot.logging.subpack.SomeClass.run(SomeClass.java:12) ~[classes!/:na]
	at playground.springboot.logging.SpringbootLoggingApplication.run(SpringbootLoggingApplication.java:25) [classes!/:na]
	at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) [spring-boot-2.1.0.RELEASE.jar!/:2.1.0.RELEASE]
	at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) [spring-boot-2.1.0.RELEASE.jar!/:2.1.0.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) [spring-boot-2.1.0.RELEASE.jar!/:2.1.0.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.0.RELEASE.jar!/:2.1.0.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.0.RELEASE.jar!/:2.1.0.RELEASE]
	at playground.springboot.logging.SpringbootLoggingApplication.main(SpringbootLoggingApplication.java:15) [classes!/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
	at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [springboot-logging-0.0.1-SNAPSHOT.jar:na]
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [springboot-logging-0.0.1-SNAPSHOT.jar:na]
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [springboot-logging-0.0.1-SNAPSHOT.jar:na]
	at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [springboot-logging-0.0.1-SNAPSHOT.jar:na]


==Format==

<syntaxhighlight lang='java'>
log.info("accessed by {} on {}", user, date);

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