SLF4J
Developer(s) | Ceki Gülcü |
---|---|
Stable release | 2.0.9
/ September 3, 2023[1] |
Repository | |
Written in | Java |
Operating system | Cross-platform |
Type | Logging Tool |
License | MIT License |
Website | www |
Simple Logging Facade for Java(SLF4J) provides aJavaloggingAPIby means of a simplefacade pattern.The underlying logging backend is determined atruntimeby adding the desired binding to theclasspathand 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 withJUnit,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,privatestaticfinalLoggerLOG=LoggerFactory.getLogger(Wombat.class);
- InLogger,the logging methods areoverloadedwith 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 expensivetoString()
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 andtoString()
method for the valuescount
oruserAccountList
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 inLoggerfor
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. - Unlikelog4j,SLF4J offers logging methods that accept markers. These are special objects that enrich the log messages. At present time,logbackis 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:
- Support forlambda syntaxin passing messages to be logged.
- Addition of aFluentAPI.
- RequiresJava 8or later.
Version 1
[edit]Version details can be foundin the manual.
Significant versions include:
- Version 1.7.36 and all newer releases arereproducible.
- 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. SeeDownloadproduct page.
- Version 1.7.5 yielded significant improvement in logger retrieval times.
- Version 1.7.0 added support forvarargs,requiring support forJava 5or later.
- Version 1.6 brought ano-operationimplementation used by default if no binding found.
- Version 1.1 releases in Maven repositoriesbegan 2006-09.
See also
[edit]References
[edit]- ^"SLF4J News".slfj4.org.2023-09-03.
- ^java.util.logging
- ^logback
- ^tinylog
- ^"SLF4J binding for tinylog".Archived fromthe originalon 2017-12-07.Retrieved2014-12-16.
- ^"Think again before adopting the commons-logging API"
- ^"Taxonomy of class loader problems encountered when using Jakarta Commons Logging"
- ^"We Analyzed 30,000 GitHub Projects – Here Are The Top 100 Libraries in Java, JS and Ruby".Archived fromthe originalon 2014-07-09.Retrieved2014-02-09.
- ^Nicolas, Frenkel.""most popular" section ".Twitter.Retrieved2022-01-16.
- ^SLF4J api docs: Logger
- ^Apache log4j 2.x slf4j Binding
- ^"SLF4J News".www.slf4j.org.Retrieved2023-11-27.