Sub topic 12
Routing `System.Logger` into SLF4J
Assume you know basic Java classes, try/catch, and how to add Maven dependencies. This chapter also assumes that you have seen log levels and understand that a logging facade separates application code from the logging backend.
An application can use the JDK's System.Logger API while sending its output to SLF4J. This is useful when libraries use System.Logger, but the application already uses SLF4J and Logback. One backend can then control both kinds of messages.
How the bridge works
System.Logger does not write directly to a file or console. The JDK asks a LoggerFinder to create the logger. Without a replacement, the JDK uses its default platform logging implementation.
The slf4j-jdk-platform-logging module provides another LoggerFinder. It forwards System.Logger calls to SLF4J. SLF4J then forwards the events to its configured provider, such as Logback.
The bridge changes the destination of System.Logger events. It does not change the source code that calls System.Logger.
Add the required dependencies
The application needs the SLF4J API, a provider, and the bridge. Logback is the provider in this example.
<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>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk-platform-logging</artifactId>
<version>2.0.16</version>
<scope>runtime</scope>
</dependency>
</dependencies>Keep the SLF4J versions aligned. The bridge must be visible at runtime, because the JDK discovers the replacement LoggerFinder when the application starts.
Write code with either API
Existing code can continue to use System.Logger:
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
public class Checkout {
private static final Logger log =
System.getLogger(Checkout.class.getName());
public static void main(String[] args) {
String orderId = "A-1042";
log.log(Level.INFO, "Starting checkout for order {0}", orderId);
try {
validate(orderId);
log.log(Level.DEBUG, "Order {0} passed validation", orderId);
} catch (IllegalArgumentException exception) {
log.log(Level.ERROR, "Checkout validation failed", exception);
}
}
private static void validate(String orderId) {
if (orderId.isBlank()) {
throw new IllegalArgumentException("The order ID is blank");
}
}
}With the bridge installed, these events pass through SLF4J and use the Logback configuration. System.Logger.Level.WARNING corresponds to the warning level, even though SLF4J calls that level WARN.
SLF4J code uses its own formatting syntax:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Inventory {
private static final Logger log =
LoggerFactory.getLogger(Inventory.class);
public void reserve(String sku, int quantity) {
log.info("Reserving {} units of {}", quantity, sku);
}
}The two APIs have different placeholders:
| API | Placeholder | Example |
|---|---|---|
| `System.Logger` | `{0}`, `{1}` | `"User {0} logged in"` |
| SLF4J | `{}` | `"User {} logged in"` |
Do not convert message patterns mechanically when moving code between APIs.
Common failure modes
If the bridge is absent, System.Logger output may use the JDK's default behavior instead of the Logback pattern. If more than one System.LoggerFinder implementation is present, the selected provider can depend on class-path or module-path configuration.
A missing SLF4J provider produces a warning and usually causes SLF4J to use a no-operation logger. The application may appear to run successfully while emitting no SLF4J messages.
When using the Java module system, make the bridge visible on the module path and verify its service-provider configuration. Test the packaged application, not only the IDE run configuration.
Choosing the arrangement
Use System.Logger when you want a JDK-only API or are writing a library with no logging dependency. Use SLF4J when your application needs broad backend support, MDC, or structured key-value fields. Add the platform bridge when both APIs occur in the same process and should share the same backend and configuration.
Exercise
An application uses Logback for SLF4J messages, but a dependency writes through System.Logger. Which dependency makes the dependency's messages follow the Logback configuration?
Show answer
Add org.slf4j:slf4j-jdk-platform-logging with a version compatible with the SLF4J API. It provides the System.LoggerFinder that forwards those messages into SLF4J.