Sub topic 8
SLF4J providers and the missing-provider warning
This chapter assumes you can add dependencies with Maven or Gradle and that you have written basic Logger calls with SLF4J. It explains how SLF4J 2.x picks a logging backend at startup and how to diagnose the three classpath problems you will meet most often: no provider, several providers, and a provider built for the wrong SLF4J version.
The API and the provider are separate jars
The slf4j-api jar contains Logger, LoggerFactory, MDC and the other types you call. It contains no code that writes log lines. The writing is done by a provider, a second jar that implements the SPI interface org.slf4j.spi.SLF4JServiceProvider.
Your code and your libraries compile against slf4j-api only. The final application adds exactly one provider at runtime. This is why a library can log without forcing a backend on the applications that use it.
| Provider artifact | Backend it connects to | Typical use |
|---|---|---|
| `ch.qos.logback:logback-classic` (1.3 or later) | Logback | General-purpose default |
| `org.apache.logging.log4j:log4j-slf4j2-impl` | Log4j 2 | Projects already on Log4j 2 |
| `org.slf4j:slf4j-jdk14` | `java.util.logging` | No extra backend library |
| `org.slf4j:slf4j-simple` | Prints to `System.err` | Small tools and demos |
| `org.slf4j:slf4j-reload4j` | Reload4j (Log4j 1.x fork) | Legacy systems |
| `org.slf4j:slf4j-nop` | Nothing | Deliberately silencing all logging |
The Log4j artifact is log4j-slf4j2-impl for SLF4J 2.x. The older log4j-slf4j-impl targets SLF4J 1.7 and is not a valid provider for 2.x.
How SLF4J finds the provider
SLF4J 2.x uses the standard java.util.ServiceLoader mechanism. The first call to LoggerFactory.getLogger(...) triggers initialization, and that works as follows:
LoggerFactoryasksServiceLoaderfor everySLF4JServiceProvidervisible on the classpath or module path.- If it finds none, it prints a warning and falls back to a no-operation implementation.
- If it finds more than one, it prints a warning, lists them, and uses the first one.
- It initializes the chosen provider, and from then on
getLoggerreturns loggers created by that provider.
A provider jar announces itself in one of two ways. On the classpath, it contains a file META-INF/services/org.slf4j.spi.SLF4JServiceProvider naming the implementation class. On the module path, its module-info.java declares provides org.slf4j.spi.SLF4JServiceProvider with ....
The missing-provider warning
Suppose a project declares only the API:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.16</version>
</dependency>and runs a class that logs with log.info(...). The program compiles and runs without an exception. It prints this on standard error and nothing else:
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details.The NOP logger accepts every call and discards it. Your log.error(...) lines vanish. This is the main danger of the warning: the application keeps working, so the message is easy to overlook, and the first sign of trouble is often an incident with no logs.
The fix is to add one provider with runtime scope, as in the dependency block from the previous chapter:
<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>The Gradle equivalent keeps the provider off the compile classpath:
dependencies {
implementation("org.slf4j:slf4j-api:2.0.16")
runtimeOnly("ch.qos.logback:logback-classic:1.5.12")
}Runtime scope matters for design, not for function. It stops your own code from importing Logback types, so you can swap backends later without touching source files.
Checking which provider is active
When the output looks wrong, ask SLF4J directly which factory it chose. Printing the class name of the logger factory tells you the provider in one line:
import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;
public class WhichProvider {
public static void main(String[] args) {
ILoggerFactory factory = LoggerFactory.getILoggerFactory();
System.out.println("Logger factory: " + factory.getClass().getName());
}
}With Logback, the output is Logger factory: ch.qos.logback.classic.LoggerContext. With no provider, it is org.slf4j.helpers.NOPLoggerFactory. With slf4j-simple, it is org.slf4j.simple.SimpleLoggerFactory. This check is cheap enough to put in a startup self-test or a debugging session.
Multiple providers
A common accident is that two libraries each pull in a different provider. For example, your application declares logback-classic, and a transitive dependency brings in slf4j-simple. SLF4J prints something like:
SLF4J: Class path contains multiple SLF4J providers.
SLF4J: Found provider [ch.qos.logback.classic.spi.LogbackServiceProvider@1b2c3d]
SLF4J: Found provider [org.slf4j.simple.SimpleServiceProvider@4e5f6a]
SLF4J: See https://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual provider is of type [ch.qos.logback.classic.spi.LogbackServiceProvider@1b2c3d]The last line is the one that counts. The choice depends on classpath order, which depends on the build tool and packaging, so it can change between your IDE and your production jar. Do not rely on the order. Remove the unwanted provider instead.
First find where it comes from:
mvn dependency:tree -Dincludes=org.slf4j,ch.qos.logbackThen exclude it from the dependency that drags it in:
<dependency>
<groupId>com.example</groupId>
<artifactId>legacy-client</artifactId>
<version>3.4.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>In Gradle, use ./gradlew dependencyInsight --dependency slf4j-simple to trace it, and exclude group: ..., module: ... to remove it.
Mismatched versions: 1.7 bindings with a 2.x API
SLF4J 1.7 and earlier used a different mechanism. Each binding contained a class named org.slf4j.impl.StaticLoggerBinder, and the API loaded it directly. SLF4J 2.0 dropped that mechanism for ServiceLoader. As a result, the API version and the provider version must belong to the same generation:
| API on classpath | Provider that works | What you see if you mix them |
|---|---|---|
| `slf4j-api` 2.0.x | `logback-classic` 1.3 or later | Works |
| `slf4j-api` 2.0.x | `logback-classic` 1.2.x | No-providers warning, plus a notice that a 1.7-style binding was found |
| `slf4j-api` 1.7.x | `logback-classic` 1.5.x | Failed to load `StaticLoggerBinder`, NOP fallback |
In the first mismatch, SLF4J 2.x detects the old StaticLoggerBinder class and prints that the classpath contains bindings targeting slf4j-api 1.7.x or earlier. It still defaults to NOP, because the old binding does not register as a ServiceLoader provider. The fix is to upgrade the provider, not to add more jars.
Spring Boot and other frameworks manage these versions through a bill of materials. If you override only one of the two versions in your own build file, you can create exactly this mismatch.
Providers on the module path
If your application is a named module, declare the requirement on the API module:
module com.example.app {
requires org.slf4j;
}Provider modules advertise themselves through provides ... with ..., and ServiceLoader finds them as long as the provider module is resolved into the module graph. A provider that is on the module path but not required by anything may not be resolved. Add it with --add-modules ch.qos.logback.classic or let your build tool put it on the path with a requires from a launcher module. When in doubt, run java --list-modules against your module path, or print the factory class as shown above.
Libraries must not ship a provider
A library should depend on slf4j-api and nothing else. If it bundles logback-classic or slf4j-simple as a normal compile dependency, every application that uses it inherits that provider, and the multiple-providers problem follows. The one exception is a test scope dependency, which Maven and Gradle do not pass on to consumers.
Summary of failure modes
| Symptom | Cause | Fix |
|---|---|---|
| "No SLF4J providers were found", no log output | Only `slf4j-api` on the runtime classpath | Add one provider with runtime scope |
| "Class path contains multiple SLF4J providers" | Two provider jars, often one transitive | Exclude all but one, or set `slf4j.provider` |
| Warning about bindings targeting 1.7.x or earlier | Provider older than the API | Upgrade the provider to the 2.x generation |
| Provider present in the build, still NOP in a module app | Provider module not resolved | Add it with `--add-modules` or require it |
Exercise
A service starts without errors, but nothing it logs appears anywhere. Standard error shows "SLF4J: Defaulting to no-operation (NOP) logger implementation". Its pom declares slf4j-api 2.0.16 and logback-classic 1.2.13. What is the likely cause, and what change fixes it?
Show answer
Logback 1.2.x is a provider for SLF4J 1.7 and uses the old StaticLoggerBinder mechanism, so SLF4J 2.x's ServiceLoader lookup finds no provider and falls back to NOP. Upgrade logback-classic to a 2.x-compatible release such as 1.5.12 (1.3 or later), keeping runtime scope. Then run LoggerFactory.getILoggerFactory().getClass().getName() to confirm it reports ch.qos.logback.classic.LoggerContext.
Exercise
Your application's startup output lists two found providers, Logback and slf4j-simple, and names Logback as the actual provider. In your IDE this is fine, but the packaged jar logs through slf4j-simple. Why, and how do you make it consistent?
Show answer
When several providers are present, SLF4J takes the first one that ServiceLoader returns, and that order depends on the classpath order, which differs between IDE runs and the packaged application. Find the source of slf4j-simple with mvn dependency:tree -Dincludes=org.slf4j, exclude it from the dependency that brings it in, and rebuild so only one provider remains. As a temporary measure you can set -Dslf4j.provider=ch.qos.logback.classic.spi.LogbackServiceProvider.