1

We have many "parent" (or "library") projects. They only have the slf4j-api in the classpath, but no implementation like logback.

I often want to write a quick temporary main method to test something, and get

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.

How can I get output without adding a logging implementation dependency?

Reto Höhener
  • 5,419
  • 4
  • 39
  • 79

3 Answers3

4

You can't get around having some logging dependency available if you want log output. What you can do, however, is to use the simple binding and set the dependency as <optional>true</optional>. This effectively prevents the dependency from being added transitively to client projects while making it available when you run a main method in that specific module.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
2

Slf4j provides APIs (interfaces), not an implementation, so without adding some implementation / rolling your own IMO it would be impossible to see something being actually printed on console and in general to get around this warning an make it work in a way you ask.

If you don't need a logback, you can use slf4j simple binding for example.

Another option is to use some kind of bridge to adapt the slf4j apis to Java Util logging and then there is an implementation in JDK (like slf4j-jdk14-1.8.0-beta4.jar)

If you want tests you can use the dependency in scope "test" (assuming you use maven, for example) so it won't propagate to the artifact.

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • How is it going to help, his question focuses on "without adding a logging implementation dependency" – Sambit May 23 '19 at 08:07
  • Adding logback or such into scope `test` is a good answer. You can run a quick ad-hoc program through tests, and adding a test-dependency does not affect downstream users of the project. – Thilo May 23 '19 at 08:27
  • @Sambit: I've mentioned that its impossible to achieve (IMO) without adding the binding from from the beginning of my answer. I'll be glad to be proven otherwise though. In any case, I've provided some alternative solutions (compromises). However, I could be probably more clear with the first statement, so I've slightly edited the answer – Mark Bramnik May 23 '19 at 09:02
0

To your question, "How can I get output without adding a logging implementation dependency?". It is not possible in SLF4J. There has to be one logging implementation. If I go by the Bible of SLF4J, it says "The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for various logging frameworks (e.g. java.util.logging, logback, log4j) allowing the end user to plug in the desired logging framework at deployment time".

As Mark mentioned, you can use .jdk14.jar file to eliminate the warning and to print. But in case of any library, you have to add it.

For more reference, you can check below.

https://www.slf4j.org/ https://www.slf4j.org/manual.html

Sambit
  • 7,625
  • 7
  • 34
  • 65