SLF4J

Simple Logging Facade for Java (SLF4J) provides a Java logging API by means of a simple facade pattern. The underlying logging backend is determined at runtime by adding the desired binding to the classpath and may be the standard Sun Java logging package java.util.logging,[2] log4j, logback[3] or tinylog.[4][5]

Simple Logging Facade for Java
Developer(s)Ceki Gülcü
Stable release
1.7.30 / December 16, 2019 (2019-12-16)[1]
Repository
Written inJava
Operating systemCross-platform
TypeLogging Tool
LicenseMIT License
Websitewww.slf4j.org

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ü as a more reliable alternative to Jakarta Commons Logging framework.[6][7] Research in 2013 on 10,000 GitHub projects found that the most popular Java library is SLF4J, along with JUnit, with 30.7% of projects using it.[8]

Similarities and differences with log4j 1.x

  • Five of log4j's six logging levels are used (ERROR, WARN, INFO, DEBUG, TRACE). 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. In addition, SLF4J markers offer a more general method for tagging log statements. For example, any log statement of level ERROR can be tagged with the "FATAL" marker.
  • 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.[9] Occurrences of the simple pattern {} in the log message are 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 given level, the logging framework does not need to evaluate the string representation of the values, or construct a log message string that is never actually logged. In the following example, string concatenation and toString() method for the values count or userAccountList are performed only when DEBUG is enabled.
 LOG.debug("There are now " + count + " user accounts: " + userAccountList); // slower
 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, SLF4J offers logging methods that accept markers. These are special objects that enrich the log messages. At present time, logback is the only framework which makes use of markers.

Similarities and differences with log4j 2.x

Apache log4j 2.x supports all slf4j features.[10]

Version history

Version 2

Version 2 is currently in development, with an alpha pre-release available. Requires Java 8 or later.

Significant feature additions:

Version 1

Version details can be found in the manual.

Significant versions include:

  • Version 1.7.30 is the current stable release. See Download product page.
  • Version 1.7.5 yielded significant improvement in logger retrieval times.
  • Version 1.6 brought a no-operation implementation used by default if no binding found.
  • Version 1.1 releases in Maven repositories began 2006-09.
gollark: > “No! ElGr cells are a scientific miracle!” cries biologist Jack Ponta, jiggling a beaker full of purplish goop as he waves his arms in exasperation. “These cells have been a breakthrough; not only in testing cures for cancer, but also in understanding how cancer develops and functions! All these years later, these cells keep chugging along, outliving all the others! Who knows, with these cells, we might even one day unlock a path to immortality! Are you going to let bureaucracy get in the way of SCIENCE?”
gollark: > “We thought my poor grandmother’s remains had been buried in accordance with her wishes,” growls Elizabeth’s direct descendant, Catherine Gratwick. “Can’t you let her rest in peace? This is her body that you’re messing with. You can’t just irradiate and poison her; you must ask me first! How would you like it if your family’s remains were exhumed and mutilated? You must never use cells from deceased people without the explicit pre-mortem consent of the patient or their relatives. As for granny - I insist that all remaining samples of her be buried, and that you financially compensate her family for the pain and grief you have caused!”
gollark: > Two generations ago, scientists took a biopsy of a tumor from a cancer patient named Elizabeth Gratwick, who died soon after. Without her knowledge or consent, these cells were preserved in the laboratory and proved to be exceptionally stable in replication. As stable cancer cell lines are highly useful for medical research, “ElGr cells” have been sent to and used by scientists all over the world. However, objections are now being raised by Elizabeth’s descendants.
gollark: Now I need to answer a question!
gollark: And top 1% for crime.

See also

References

This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.