SLF4J
Encyclopedia
Simple Logging Facade for Java (SLF4J) provides a Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 logging API by means of a simple facade pattern
Façade pattern
The facade pattern is a software engineering design pattern commonly used with Object-oriented programming. The name is by analogy to an architectural facade....

. The underlying logging backend is determined at deployment time and may be java.util.logging, log4j
Log4j
Apache log4j is a Java-based logging utility. It was originally written by Ceki Gülcü and is now a project of the Apache Software Foundation. log4j is one of several Java Logging Frameworks....

 or logback.

The separation of the client API from the logging backend reduces the coupling between an application and any particular logging framework. This can make it easier to integrate with existing or third-party code or to deliver code into other projects that have already made a choice of logging backend.

SLF4J was created by Ceki Gülcü
Ceki Gülcü
Ceki Gülcü is the founder of the log4j project and the author of The complete log4j manual. He has since started the SLF4J and Logback projects, with the intention of offering a compatible successor to log4j....

 as a more reliable alternative to Jakarta Commons Logging framework.

Similarities and Differences with Log4j

  • Five of Log4j's six logging levels are used. FATAL has been dropped on the basis that inside the logging framework is not the place to decide when an application should terminate and therefore there is no difference between ERROR and FATAL from the logger's point of view.

  • Logger instances are created via the LoggerFactory, which is very similar in Log4j. For example,


private static final Logger LOG = LoggerFactory.getLogger(Wombat.class);
  • In Logger, the logging methods are overloaded with forms that accept one, two or more values. Occurrences of the simple pattern {} in the log message is replaced in turn with the values. This is simple to use yet provides a performance benefit when the values have expensive toString methods. When logging is disabled at the DEBUG level, the logging framework does not need to evaluate the string representation of the values. In the following example, the values count or userAccountList only need to be evaluated when DEBUG is enabled; otherwise the overhead of the debug call is trivial.


LOG.debug("There are now " + count + " user accounts: " + userAccountList); // slow
LOG.debug("There are now {} user accounts: {}", count, userAccountList); // faster
  • Similar methods exist in Logger for isDebugEnabled etc. to allow more complex logging calls to be wrapped so that they are disabled when the corresponding level is disabled, avoiding unnecessary processing.

  • Unlike Log4j
    Log4j
    Apache log4j is a Java-based logging utility. It was originally written by Ceki Gülcü and is now a project of the Apache Software Foundation. log4j is one of several Java Logging Frameworks....

    , SLF4J offers logging methods that accept markers. These are special objects that enrich the log messages and are an idea that SLF4J has borrowed from logback.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK