Sub topic 8
SLF4J Providers and the Missing-Provider Warning
This chapter assumes that you know basic Java classes, Maven or Gradle dependencies, and try/catch. You should also understand that an interface defines operations while another class supplies their implementation.
The API is not the logging system
SLF4J is a logging facade. A facade is an API that application code calls without choosing the implementation that writes log events.
Your code depends on slf4j-api:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CheckoutService {
private static final Logger log =
LoggerFactory.getLogger(CheckoutService.class);
public void start(String orderId) {
log.info("Starting checkout for order {}", orderId);
}
}The Logger interface and LoggerFactory come from slf4j-api. They do not decide whether messages go to the console, a file, or another destination. An SLF4J provider supplies that implementation at runtime.
Common providers include:
| Provider | Typical use |
|---|---|
| Logback Classic | General applications using Logback |
| `slf4j-simple` | Small programs with simple console output |
| `slf4j-jdk14` | Applications that want Java Util Logging |
The API belongs on the compile classpath. The provider must be available when the application runs.
What the missing-provider warning means
With SLF4J 2.x, the provider is discovered through Java's service-provider mechanism. At startup, LoggerFactory searches the runtime classpath or module path for an SLF4J provider.
If it finds none, it prints a warning similar to this:
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementationA no-operation logger, often called a NOP logger, accepts log calls and discards them. Your application may continue running, but its log messages disappear.
The usual fix is to add both the API and a provider. For Logback, Maven dependencies can look like this:
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.16</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.12</version>
<scope>runtime</scope>
</dependency>
</dependencies>logback-classic provides the SLF4J implementation and normally brings the required Logback core library with it. The runtime scope documents that application code compiles against the facade while Logback is needed to run the application.
Version compatibility matters
SLF4J 2.x uses the provider mechanism. An old SLF4J 1.7 binding, such as a dependency designed for StaticLoggerBinder, is not a valid provider for SLF4J 2.x. SLF4J may report that it found bindings targeting an older version and still fall back to NOP logging.
Keep the major versions aligned:
- Use
slf4j-api2.x with a provider designed for SLF4J 2.x. - Use
slf4j-api1.7 with a 1.7-compatible binding only when maintaining an older application. - Check the dependency tree when a transitive dependency brings an unexpected version.
With Maven, inspect the resolved dependencies using:
mvn dependency:tree -Dincludes=org.slf4j,ch.qos.logbackMultiple providers
If two providers are present, SLF4J reports a multiple-provider warning. It selects one provider, but selection based on classpath order is not a reliable configuration choice. The selected backend can change after a packaging or dependency change.
Keep one provider in the final application. Libraries should depend on slf4j-api, not on Logback or another provider, unless they have a specific integration requirement.
Check the runtime, not only compilation
A project can compile successfully while having no provider. Compilation proves that slf4j-api is available. It does not prove that a provider is packaged in the runtime image, container, or test process.
Test the same artifact and launch command used in deployment. Treat a missing-provider warning as a configuration defect unless deliberately using a NOP logger.
Exercise
Why can an application compile successfully but lose every SLF4J message at runtime?
Show answer
Compilation needs only slf4j-api. If no compatible provider is present at runtime, SLF4J uses its NOP logger and discards the messages.