Logging in Java

  • SLF4J: SLF4J stands for Simple Facade For Java. It is nothing but only a facade for logging system. It doesn’t do the logging implementation work. It is logging design pattern.

If you only include SLF4J.jar in your project, what messages will you get? Here is very simple and easy understanding example from SLF4J. You will get those warning messages:

<!-- wp:shortcode -->
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.
<!-- /wp:shortcode -->

Simply because there is no logger implementation component.

  • Log4J / Logback:

Logger4J or Logback are Java logging Framework who implementation SLF4J.

Here is every good picture from SLF4J manual: to help you understand the layers between log4j/logback/ other framework and SLF4J.

  • Use Cases:

When you design a Java Library which will be included and used by other project, you should only include SLF4J in this library and you give the chances to the using projects who choice the Java Logging Framework.

If you written a Service and Application, you should use SLF4J + Log4j or SLF4J + Logback etc.

In short, libraries and other embedded components should consider SLF4J for their logging needs because libraries cannot afford to impose their choice of logging framework on the end-user. On the other hand, it does not necessarily make sense for stand-alone applications to use SLF4J. Stand-alone applications can invoke the logging framework of their choice directly. In the case of logback, the question is moot because logback exposes its logger API via SLF4J.

From slf4j FAQ

Author: aerodc

Software Engineer