skip to content

In SLF4J 2.0, how do calls to the JDK's System.Logger reach SLF4J, and does a Spring application still need jcl-over-slf4j for Commons Logging?

level: middleimportance: nice to knowfreq 27%

answer

  1. JEP 264's service lookup
  2. a LoggerFinder in META-INF/services
  3. the SLF4J backend decides isLoggable
  4. read spring-core's own dependency first
  5. two jars, one org.apache.commons.logging

basics

~10 s

Add slf4j-jdk-platform-logging: its SLF4JSystemLoggerFinder is the JDK's System.LoggerFinder service and hands every System.Logger to SLF4J. Spring Framework 7.0.9's spring-core already depends on Apache commons-logging 1.3.5, so check that before adding jcl-over-slf4j.

solid answer

~40 s

The JDK's platform logging API (JEP 264) looks up a `System.LoggerFinder` service; `slf4j-jdk-platform-logging`, new in the 2.0 line, registers `SLF4JSystemLoggerFinder` for it, both in `META-INF/services` and in its `module-info`. Each `System.getLogger(name)` then returns an adapter over `LoggerFactory.getLogger(name)`, and `isLoggable` asks the SLF4J logger, so the bound backend's level decides. Commons Logging is different: `jcl-over-slf4j` re-implements the `org.apache.commons.logging` classes over SLF4J. In Spring Framework 7.0.9, `spring-core` declares `commons-logging:commons-logging`, pinned to 1.3.5 in the framework platform, and there is no `spring-jcl` module. So look at what that real Commons Logging jar does with SLF4J before adding `jcl-over-slf4j`; the two jars carry the same package and should not both be present.

code

java · 6 lines
java
System.Logger platform = System.getLogger("com.example.billing");
platform.log(System.Logger.Level.DEBUG, "retry {0}", 3);

org.apache.commons.logging.Log jcl =
        org.apache.commons.logging.LogFactory.getLog("com.example.billing");
jcl.info("settled");

go deeper

for a junior

Recall that the JDK has its own System.Logger API, that SLF4J has a jar to route it, and that Spring logs through Commons Logging.

for a middle

Explain the LoggerFinder service lookup, that the SLF4J backend alone decides isLoggable, and that jcl-over-slf4j replaces the Commons Logging classes rather than adding to them.

for a senior

Check spring-core's actual dependency before adding jars, keep exactly one org.apache.commons.logging provider, and verify each entry point with a probe log line.

for a principal

Decide which legacy entry points a service template routes by default, and record why, so teams stop copying bridge lists from guides written for older Spring generations.

## Two APIs that do not need a classic bridge The legacy-bridge story is usually told with Log4j 1 and java.util.logging. Two other entry points matter in a modern JVM service, and both work differently: - the JDK's **System.Logger**, the platform logging API added by JEP 264 in Java 9, which JDK code and some libraries call through `System.getLogger(name)`; - **Apache Commons Logging** (JCL), the old facade that Spring Framework's core logs through. ## System.Logger: a service the JDK looks up JEP 264 lets the JDK delegate `System.Logger` to a pluggable `System.LoggerFinder`. The JDK locates that finder as a service, so no handler has to be installed at run time, unlike the JUL bridge. The `slf4j-jdk-platform-logging` artifact, whose pom describes it as integrating SLF4J "with the Platform Logging API added by JEP 264 in Java 9", plugs in like this: 1. `META-INF/services/java.lang.System$LoggerFinder` names `org.slf4j.jdk.platform.logging.SLF4JSystemLoggerFinder`, and its `module-info` says `provides java.lang.System.LoggerFinder with` that class, so it is found on the class path and the module path. 2. `SLF4JSystemLoggerFinder` (marked `@since 2.0.0`) extends `System.LoggerFinder`; its `getLogger(name, module)` ignores the module and returns an adapter wrapping `LoggerFactory.getLogger(name)`. 3. The adapter, `SLF4JPlatformLogger`, implements `System.Logger` and forwards each call to the SLF4J logger through a logging-event builder, with `System.Logger` as the caller boundary. JDK-style `{0}` parameters are formatted with `MessageFormat.format` before the call, and each parameter is also attached with `addArgument` for a backend that wants it. If no such finder is present, the JDK falls back to its default, which writes through java.util.logging when that module is available; then only a JUL bridge would carry those records to SLF4J. ### Level mapping and who decides | `System.Logger.Level` | SLF4J level | |---|---| | `TRACE` | TRACE | | `DEBUG` | DEBUG | | `INFO` | INFO | | `WARNING` | WARN | | `ERROR` | ERROR | | `ALL` | TRACE | | `OFF` | ERROR | `ALL` and `OFF` are not meant for printing calls; the adapter's `fixExtremeLevels` maps them to TRACE and ERROR instead of failing. More important, `isLoggable(level)` and every `log(...)` call ask `slf4jLogger.isEnabledForLevel(...)`. The **SLF4J backend's level is the only gate**, which is the opposite of the JUL bridge, where JUL's own logger level filters first. ## Platform finder or JUL bridge The two cover different APIs and can sit side by side: - with the finder installed, `System.Logger` calls go straight to SLF4J and never touch java.util.logging; - code that calls `java.util.logging.Logger` directly is unaffected by the finder and still needs `jul-to-slf4j`; - neither forms a loop with the other, because the finder never writes to JUL and the JUL bridge never calls `System.Logger`. ## Commons Logging in a Spring application `jcl-over-slf4j` ("JCL 1.2 implemented over SLF4J" in its pom) takes the replacement route: it ships its own `org.apache.commons.logging.LogFactory`, which always delegates to `org.apache.commons.logging.impl.SLF4JLogFactory`, plus `Log` and a few implementations. A handful of JCL 1.1 protected methods throw `UnsupportedOperationException` with a pointer to SLF4J's codes page. Because it defines the same classes as the real Commons Logging jar, the two should never both be on a classpath: which `LogFactory` wins would depend on class-path order. What Spring brings decides whether you need it at all. In Spring Framework 7.0.9: - `spring-core/spring-core.gradle` declares `api("commons-logging:commons-logging")`; - `framework-platform/framework-platform.gradle` pins it to `commons-logging:commons-logging:1.3.5`; - the build has no `spring-jcl` module, the adapter earlier Spring generations shipped. So a Spring 7 application gets the real Apache Commons Logging jar transitively. The 1.3 line of Commons Logging is documented as discovering SLF4J by itself; that mechanism is outside SLF4J's source, so confirm it in the Commons Logging documentation for the version you resolve rather than taking it as given. ## A practical check - Read the resolved dependency tree for `commons-logging` and `jcl-over-slf4j`; keep exactly one of them. - Emit one line through `org.apache.commons.logging.LogFactory.getLog(...)` and one through `System.getLogger(...)` and confirm both reach your backend's output with the expected logger names. - If you add `slf4j-jdk-platform-logging`, remember that `System.Logger` levels are now controlled only in the SLF4J backend. - Do not reach for `jcl-over-slf4j` by habit from older Spring guides; it replaces, rather than complements, the jar Spring now depends on.

  • With slf4j-jdk-platform-logging installed, where do you change the level of a System.Logger named com.example.billing?
    In the bound SLF4J backend's configuration for the logger `com.example.billing`. `SLF4JPlatformLogger.isLoggable` and `log` call `isEnabledForLevel` on the SLF4J logger of the same name, so there is no separate JDK-side level to adjust, unlike the JUL bridge, where JUL filters first.
  • Does adding slf4j-jdk-platform-logging also capture code that calls java.util.logging.Logger directly?
    No. The finder only answers the JDK's `System.LoggerFinder` lookup, so it covers `System.getLogger(...)` callers. Code written against `java.util.logging.Logger` still logs into JUL and needs `jul-to-slf4j` with `SLF4JBridgeHandler` installed. The two can coexist without a loop, because the finder never writes to JUL.

  • slf4j-jdk-platform-logging must be installed with a call at startup.
  • A System.Logger level still has to be lowered on the JDK side.
  • Spring Framework 7 logs through its own spring-jcl module.
  • Every Spring application needs jcl-over-slf4j to reach SLF4J.
  • Keeping both jcl-over-slf4j and commons-logging is harmless.
  • System.Logger calls at Level.OFF are discarded by the SLF4J adapter.

Assumes SLF4J 2.0.20, whose SLF4JSystemLoggerFinder is marked since 2.0.0, and Spring Framework 7.0.9, whose spring-core depends on commons-logging 1.3.5.