Thursday, April 26, 2012

Simple Logging Facade for Java (SLF4J)

             The Simple Logging Facade for Java or (SLF4J) serves as a simple facade or abstraction for various logging frameworks, e.g. java.util.logging, log4j and logback, allowing the end user to plug in the desired logging framework at deployment time.
Before you start using SLF4J, we highly recommend that you read the two-page SLF4J user manual.

Note that SLF4J-enabling your library implies the addition of only a single mandatory dependency, namely slf4j-api.jar. If no binding is found on the class path, then SLF4J will default to a no-operation implementation.

SLF4J and and binding against a backend logger
You can only bind against one backend logging framework. With no binding framework on the classpath, the silent logger (NOP) will be used by default.
The frameworks supported by SLF4J:
  • Logback-classic
  • Log4J
  • java.util.logging (JUL)
  • Simple
  • NOP
  • Jakarta Commons Logging (JCL)
The preferred backend logging framework with SLF4J is Logback.

//TestSlf4jClient.java
 


Check all the Project Explorer:
Note: slf4j-api-X.x.x.jar API only exposed to Application by abstracting the back-end logging framework.

 Logback
Logback is written by the same people who have written SLF4J. It natively implements the SLF4J API.
Their description of the framework:
The logback-classic module can be assimilated to a significantly improved version of log4j
A nice feature with Logback is that you can see which jar file that contains a class in the stacktrace. And if the jar file contains an Implemented-Version property in the /META-INF/MANIFEST.MF file, then you can see the version number to the right for the jar file name.

Check out all back-end logging framework outputs:
Logback-Output :
Log4J-Output:

Simple-Output: 
 NOP-Output:
  java.util.logging (JUL)-Output:

No comments: