Jump to content

SLF4J: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Update current version info.
 
(28 intermediate revisions by 17 users not shown)
Line 7: Line 7:
| caption =
| caption =
| developer = Ceki Gülcü
| developer = Ceki Gülcü
| latest release version = 1.7.30
| latest release version = 2.0.9
| latest release date = {{release date and age|2019|12|16}}<ref>{{Cite web |url=https://www.slf4j.org/news.html |title=SLF4J News |website=slfj4.org |language=en |date=2019-06-25}}</ref>
| latest release date = {{release date and age|2023|09|03}}<ref>{{Cite web |url=https://www.slf4j.org/news.html |title=SLF4J News |website=slfj4.org |language=en |date=2023-09-03}}</ref>
| latest preview version =
| latest preview version =
| latest preview date =
| latest preview date =
Line 18: Line 18:
}}
}}


'''Simple Logging Facade for Java''' ('''SLF4J''') provides a [[Java (programming language)|Java]] logging [[API]] by means of a simple [[facade pattern]]. The underlying logging backend is determined at [[Run time (program lifecycle phase)|runtime]] by adding the desired binding to the [[Classpath (Java)|classpath]] and may be the standard Sun Java logging package java.util.logging,<ref>[http://java.sun.com/javase/6/docs/api/java/util/logging/package-summary.html java.util.logging]</ref> [[log4j]], logback<ref>[http://logback.qos.ch/ logback]</ref> or tinylog.<ref>[http://www.tinylog.org/ tinylog]</ref><ref>[http://www.tinylog.org/news#29 SLF4J binding for tinylog]</ref>
'''Simple Logging Facade for Java''' ('''SLF4J''') provides a [[Java (programming language)|Java]] logging [[API]] by means of a simple [[facade pattern]]. The underlying logging backend is determined at [[Run time (program lifecycle phase)|runtime]] by adding the desired binding to the [[Classpath (Java)|classpath]] and may be the standard Sun Java logging package java.util.logging,<ref>[http://java.sun.com/javase/6/docs/api/java/util/logging/package-summary.html java.util.logging]</ref> [[Log4j]], Reload4j, Logback<ref>[http://logback.qos.ch/ logback]</ref> or tinylog.<ref>[http://www.tinylog.org/ tinylog]</ref><ref>{{Cite web |url=http://www.tinylog.org/news#29 |title=SLF4J binding for tinylog |access-date=2014-12-16 |archive-date=2017-12-07 |archive-url=https://web.archive.org/web/20171207070817/http://www.tinylog.org/news#29 |url-status=dead }}</ref>


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.
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.<ref>[http://articles.qos.ch/thinkAgain.html "Think again before adopting the commons-logging API"]</ref><ref>[http://articles.qos.ch/classloader.html "Taxonomy of class loader problems encountered when using Jakarta Commons Logging"]</ref> 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.<ref>{{cite web|url=http://www.takipiblog.com/2013/11/20/we-analyzed-30000-github-projects-here-are-the-top-100-libraries-in-java-js-and-ruby/ |title=We Analyzed 30,000 GitHub Projects – Here Are The Top 100 Libraries in Java, JS and Ruby}}</ref>
SLF4J was created by Ceki Gülcü as a more reliable alternative to Jakarta Commons Logging framework.<ref>[http://articles.qos.ch/thinkAgain.html "Think again before adopting the commons-logging API"]</ref><ref>[http://articles.qos.ch/classloader.html "Taxonomy of class loader problems encountered when using Jakarta Commons Logging"]</ref> 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.<ref>{{cite web |url=http://www.takipiblog.com/2013/11/20/we-analyzed-30000-github-projects-here-are-the-top-100-libraries-in-java-js-and-ruby/ |title=We Analyzed 30,000 GitHub Projects – Here Are The Top 100 Libraries in Java, JS and Ruby |access-date=2014-02-09 |archive-date=2014-07-09 |archive-url=https://web.archive.org/web/20140709221250/http://www.takipiblog.com/2013/11/20/we-analyzed-30000-github-projects-here-are-the-top-100-libraries-in-java-js-and-ruby/ |url-status=dead }}</ref> In January 2021, it was ranked as the second most popular project according to mvnrepository.<ref>{{Cite web|last=Nicolas|first=Frenkel|title="most popular" section|url=https://twitter.com/nicolas_frankel/status/1350428729269510145|access-date=2022-01-16|website=Twitter|language=en}}</ref>


==Similarities and differences with log4j 1.x==
==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.
*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 <code>LoggerFactory</code>, which is very similar in log4j. For example,<source lang="java">
*Logger instances are created via the <code>LoggerFactory</code>, which is very similar in log4j. For example,<syntaxhighlight lang="java">
private static final Logger LOG = LoggerFactory.getLogger(Wombat.class);
private static final Logger LOG = LoggerFactory.getLogger(Wombat.class);
</syntaxhighlight>
</source>
*In ''Logger'', the logging methods are [[Function overloading|overloaded]] with forms that accept one, two or more values.<ref>[https://slf4j.org/apidocs/org/slf4j/Logger.html SLF4J api docs: Logger]</ref> Occurrences of the simple pattern <code>{}</code> 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 <code>toString()</code> 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 <code>toString()</code> method for the values <code>count</code> or <code>userAccountList</code> are performed only when DEBUG is enabled.
*In ''Logger'', the logging methods are [[Function overloading|overloaded]] with forms that accept one, two or more values.<ref>[https://slf4j.org/apidocs/org/slf4j/Logger.html SLF4J api docs: Logger]</ref> Occurrences of the simple pattern <code>{}</code> 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 <code>toString()</code> 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 <code>toString()</code> method for the values <code>count</code> or <code>userAccountList</code> are performed only when DEBUG is enabled.
<source lang="java">
<syntaxhighlight lang="java">
LOG.debug("There are now " + count + " user accounts: " + userAccountList); // slower
LOG.debug("There are now " + count + " user accounts: " + userAccountList); // slower
LOG.debug("There are now {} user accounts: {}", count, userAccountList); // faster
LOG.debug("There are now {} user accounts: {}", count, userAccountList); // faster
</syntaxhighlight>
</source>
*Similar methods exist in ''Logger'' for <code>isDebugEnabled()</code> etc. to allow more complex logging calls to be wrapped so that they are disabled when the corresponding level is disabled, avoiding unnecessary processing.
*Similar methods exist in ''Logger'' for <code>isDebugEnabled()</code> 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.
*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.
Line 44: Line 44:
=== Version 2 ===
=== Version 2 ===


Version 2.0.0 was released on 2022-08-20. The latest version 2 release is 2.0.9 (2023-09-03).<ref>{{Cite web |title=SLF4J News |url=https://www.slf4j.org/news.html |access-date=2023-11-27 |website=www.slf4j.org}}</ref>
Version 2 is currently in development, with an [https://mvnrepository.com/artifact/org.slf4j/slf4j-api/2.0.0-alpha1 alpha pre-release available]. Requires [[Java_version_history#Java_SE_8|Java 8]] or later.


Significant feature additions:
Significant changes and feature additions:


* Support for [[Java_syntax#Lambda_expressions|lambda syntax]] in passing messages to be logged.
* Support for [[Java syntax#Lambda expressions|lambda syntax]] in passing messages to be logged.
* [[Fluent_interface|Fluent]] API.
* Addition of a [[Fluent interface|Fluent]] API.
* Requires [[Java version history#Java SE 8|Java 8]] or later.


=== Version 1 ===
=== Version 1 ===
Line 57: Line 58:
Significant versions include:
Significant versions include:


* Version 1.7.36 and all newer releases are [[Reproducible builds|reproducible]].
* Version 1.7.30 is the current stable release. See [http://www.slf4j.org/download.html ''Download''] product page.
* Version 1.7.35 slf4j-log4j12 module is replaced by slf4j-reload4j.
* Version 1.7.33 adds support for reload4j via the slf4j-reload4j module. It is the latest stable release. See [http://www.slf4j.org/download.html ''Download''] product page.
* Version 1.7.5 yielded significant improvement in logger retrieval times.
* Version 1.7.5 yielded significant improvement in logger retrieval times.
* Version 1.7.0 added support for [http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html varargs], requiring support for [[Java_version_history#J2SE_5.0|Java 5]] or later.
* Version 1.7.0 added support for [http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html varargs], requiring support for [[Java version history#Java SE 5|Java 5]] or later.
* Version 1.6 brought a [[NOP_(code)|no-operation]] implementation used by default if no binding found.
* Version 1.6 brought a [[NOP (code)|no-operation]] implementation used by default if no binding found.
* Version 1.1 releases in Maven repositories [https://mvnrepository.com/artifact/org.slf4j/slf4j-api/1.1.0-beta0 began 2006-09].
* Version 1.1 releases in Maven repositories [https://mvnrepository.com/artifact/org.slf4j/slf4j-api/1.1.0-beta0 began 2006-09].


==See also==
==See also==
Line 74: Line 77:


{{DEFAULTSORT:Slf4j}}
{{DEFAULTSORT:Slf4j}}
[[Category:Java (programming language)]]
[[Category:Java (programming language) libraries]]
[[Category:Software using the MIT license]]
[[Category:Software using the MIT license]]
[[Category:Free software programmed in Java (programming language)]]
[[Category:Log file formats]]
[[Category:Log file formats]]

Latest revision as of 18:06, 30 December 2023

Simple Logging Facade for Java
Developer(s)Ceki Gülcü
Stable release
2.0.9 / September 3, 2023; 8 months ago (2023-09-03)[1]
Repository
Written inJava
Operating systemCross-platform
TypeLogging Tool
LicenseMIT License
Websitewww.slf4j.org

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, Reload4j, Logback[3] or tinylog.[4][5]

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] In January 2021, it was ranked as the second most popular project according to mvnrepository.[9]

Similarities and differences with log4j 1.x[edit]

  • 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.[10] 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[edit]

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

Version history[edit]

Version 2[edit]

Version 2.0.0 was released on 2022-08-20. The latest version 2 release is 2.0.9 (2023-09-03).[12]

Significant changes and feature additions:

Version 1[edit]

Version details can be found in the manual.

Significant versions include:

  • Version 1.7.36 and all newer releases are reproducible.
  • Version 1.7.35 slf4j-log4j12 module is replaced by slf4j-reload4j.
  • Version 1.7.33 adds support for reload4j via the slf4j-reload4j module. It is the latest stable release. See Download product page.
  • Version 1.7.5 yielded significant improvement in logger retrieval times.
  • Version 1.7.0 added support for varargs, requiring support for Java 5 or later.
  • 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.

See also[edit]

References[edit]

  1. ^ "SLF4J News". slfj4.org. 2023-09-03.
  2. ^ java.util.logging
  3. ^ logback
  4. ^ tinylog
  5. ^ "SLF4J binding for tinylog". Archived from the original on 2017-12-07. Retrieved 2014-12-16.
  6. ^ "Think again before adopting the commons-logging API"
  7. ^ "Taxonomy of class loader problems encountered when using Jakarta Commons Logging"
  8. ^ "We Analyzed 30,000 GitHub Projects – Here Are The Top 100 Libraries in Java, JS and Ruby". Archived from the original on 2014-07-09. Retrieved 2014-02-09.
  9. ^ Nicolas, Frenkel. ""most popular" section". Twitter. Retrieved 2022-01-16.
  10. ^ SLF4J api docs: Logger
  11. ^ Apache log4j 2.x slf4j Binding
  12. ^ "SLF4J News". www.slf4j.org. Retrieved 2023-11-27.

External links[edit]