Mastering Java Logging: Navigate Log4j, SLF4J, Logback, and JUL
This guide explains the relationships and dependencies among Java logging frameworks such as Log4j, SLF4J, Logback, and JUL, shows how to resolve common issues like missing logs or jar conflicts, and provides practical steps to unify logging output across different modules and libraries.
Overview
The purpose is to clarify the relationships among various Java logging libraries, how they work, dependencies, and how to solve issues like missing logs or jar conflicts, and how to unify logging output across frameworks.
Log System
Common logging jars include commons-logging.jar, log4j.jar, slf4j-api.jar, logback.jar, etc. Understanding their relationships is essential before configuring them.
Background/History
log4jbecame the de facto standard.
Apache tried to integrate log4j into JDK; Sun refused, leading to the addition of JUL (java.util.logging) in JDK 1.4.
JUL is widely used, but switching to other frameworks requires code changes.
Apache created JCL (Jakarta Commons Logging) providing a generic API without implementation.
Log4j author created slf4j as a façade, and logback as a high‑performance implementation to replace log4j.
Apache later released log4j2 based on logback improvements.
Relationships/Dependencies
JCL is discontinued. SLF4J provides adapters for various implementations:
slf4j + logback ( slf4j-api.jar + logback-classic.jar + logback-core.jar)
slf4j + log4j ( slf4j-api.jar + slf4j-log4j12.jar + log4j.jar)
slf4j + JUL ( slf4j-api.jar + slf4j-jdk14.jar)
slf4j with no implementation ( slf4j-api.jar + slf4j-nop.jar)
SLF4J Adaptation
SLF4J can adapt any logging implementation via specific bridge jars:
JCL → jcl-over-slf4j.jar log4j → log4j-over-slf4j.jar JUL →
jul-to-slf4j.jarUnifying Spring Logging
Spring uses JCL as its façade. To make Spring use Logback through SLF4J, add jcl-over-slf4j.jar to the classpath.
Adaptation Steps
Identify the logging framework used by each module.
Include the appropriate SLF4J bridge.
Remove unused logging implementations.
Common Issues
SLF4J logs its binding status at startup. Typical problems include:
Failed to load class org.slf4j.impl.StaticLoggerBinder
Indicates no logging implementation found, often due to version incompatibility.
Multiple bindings
Multiple implementations are present; SLF4J picks one.
Code Standards
Use SLF4J API instead of direct Log4j or Logback APIs. Example: import org.slf4j.Logger; import org.slf4j.LoggerFactory; private static final Logger logger = LoggerFactory.getLogger(Abc.class);
Conclusion
The article maps Java logging components, their relationships, and provides guidance for resolving common logging problems.
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
