Comprehensive Overview of Java Logging Frameworks and SLF4J Integration
This article provides a detailed overview of Java logging frameworks—including Log4j, Log4j 2, Commons Logging, SLF4J, Logback, and JUL—covers their history, relationships, implementation mechanisms, practical selection guidelines, SLF4J usage patterns, source‑code analysis, bridge solutions for legacy APIs, and strategies for excluding unwanted logging dependencies.
Logging is an essential part of any Java application for troubleshooting, monitoring, and analytics. The most commonly used Java logging frameworks are Log4j 1, Log4j 2, Commons Logging, SLF4J, Logback, and Java Util Logging (JUL).
Common Categories
Log4j – the original Apache logging library.
Log4j 2 – the upgraded version with additional features.
Commons Logging – a generic logging API from the Apache foundation.
SLF4J – a lightweight façade that delegates to an underlying implementation.
Logback – a native implementation of the SLF4J façade.
JUL – the official logging API bundled with Java since version 1.4.
History
Log4j originated in 1996 as a tracing API and later became an Apache project. JUL appeared with Java 1.4, while Commons Logging and SLF4J were introduced to provide façade‑style abstraction. In 2006, Ceki Gülcü left Apache and created SLF4J and Logback, leading to the current two major camps: Commons Logging and SLF4J.
Relationships
Log4j 2 is not backward compatible with Log4j 1. Commons Logging and SLF4J act as façades, while Log4j and Logback are concrete implementations. Typical pairings are SLF4J + Logback or Commons Logging + Log4j.
Implementation Mechanisms
Commons Logging uses a dynamic class‑loader lookup at runtime, which can cause issues in OSGi environments. SLF4J performs static binding during compilation by locating org.slf4j.impl.StaticLoggerBinder on the classpath.
Project Selection
For new projects, the recommended combination is SLF4J with Logback because SLF4J imposes fewer restrictions and Logback offers superior performance (e.g., log‑statement checks in 3 ns vs. 30 ns for Log4j) and faster logger creation.
SLF4J Usage
SLF4J follows the Facade pattern, providing only the API jar ( slf4j-api-version.jar). Specific implementations are added via bridge jars such as logback-classic, slf4j-log4j12, etc.
1 # Using Commons Logging (higher overhead)
2 if (log.isDebugEnabled()) {
3 log.debug("User name: " + user.getName() + " buy goods id: " + good.getId());
4 }
5
6 # Using SLF4J (lower overhead)
7 log.debug("User name: {} , buy goods id: {}", user.getName(), good.getId());SLF4J Source Code Analysis
The core Maven dependency is:
1 <dependencies>
2 <!-- only slf4j-api dependency -->
3 <dependency>
4 <groupId>org.slf4j</groupId>
5 <artifactId>slf4j-api</artifactId>
6 <version>1.7.13</version>
7 </dependency>
8 </dependencies>When combined with Logback, additional dependencies are added:
1 <dependency>
2 <groupId>ch.qos.logback</groupId>
3 <artifactId>logback-classic</artifactId>
4 <version>1.2.3</version>
5 </dependency>The initialization flow of LoggerFactory includes locating the StaticLoggerBinder, binding to the chosen implementation, and creating a logger instance. Detailed step‑by‑step diagrams illustrate each method call (e.g., performInitialization(), bind(), reportMultipleBindingAmbiguity()).
Bridging Legacy APIs
To unify logging across components that use different frameworks (e.g., Spring’s Commons Logging or XSocket’s JUL), SLF4J bridges (log4j‑over‑slf4j, jcl‑over‑slf4j, jul‑to‑slf4j) redirect those calls to the SLF4J façade. Care must be taken to avoid circular dependencies that can cause infinite loops.
Excluding Unwanted Logging Dependencies
Three Maven‑based strategies are presented:
Use <exclusion> to remove commons-logging from transitive dependencies.
Declare commons-logging with provided scope and add the appropriate SLF4J bridge.
Introduce a dummy version (e.g., 99.0-does-not-exist) in a private repository to force exclusion.
Conclusion
Due to historical reasons, JUL arrived later than third‑party frameworks, and after years of evolution the two dominant ecosystems are SLF4J + Logback and Commons Logging + Log4j. New projects should choose the combination that best fits their performance and compatibility requirements.
References
SLF4J official site: http://www.slf4j.org/
SLF4J manual: http://www.slf4j.org/manual.html
Logback site: http://logback.qos.ch/
Commons Logging site: https://commons.apache.org/proper/commons-logging
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Beike Product & Technology
As Beike's official product and technology account, we are committed to building a platform for sharing Beike's product and technology insights, targeting internet/O2O developers and product professionals. We share high-quality original articles, tech salon events, and recruitment information weekly. Welcome to follow us.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
