Sub topic 4
How System.Logger Finds Its Backend
This chapter assumes basic Java classes, exception handling, and the use of Maven or Gradle dependencies. Familiarity with Java modules helps, but you can follow the example without it.
System.Logger is a logging facade built into the JDK. A facade is an API that application code calls without knowing which logging implementation will write the message. The facade keeps application code separate from the logging backend.
A backend decides where messages go. It might write to the console, a file, or a monitoring system. The backend also controls formatting, filtering, and rotation.
The LoggerFinder service
When code calls System.getLogger("name"), the JDK does not directly construct one fixed logger class. It asks a System.LoggerFinder to provide the logger.
The process is:
- Application code calls
System.getLogger. Systemobtains the activeLoggerFinder.- The finder creates or returns a logger for the requested name.
- That logger sends events to its backend.
The JDK supplies a default finder. In a normal Java runtime, the default implementation connects System.Logger to the java.util.logging system. This means a simple Java 9 application can log without adding a dependency.
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
public class BackendDemo {
private static final Logger LOG =
System.getLogger(BackendDemo.class.getName());
public static void main(String[] args) {
LOG.log(Level.INFO, "The application started");
LOG.log(Level.WARNING, "This is a warning");
System.out.println("Logger implementation: "
+ LOG.getClass().getName());
}
}Compile and run it with a Java 9-or-newer JDK:
javac BackendDemo.java
java BackendDemoThe implementation class name is an internal detail. It can differ between JDK vendors and releases. Use it for diagnosis, not for application logic.
Replacing the default finder
LoggerFinder is a service-provider interface, commonly called an SPI. An SPI lets another library provide an implementation that the platform discovers at runtime.
A replacement finder can be registered through Java's service-provider mechanism:
- A named module declares that it
provides java.lang.System.LoggerFinderwith an implementation. - A class-path application can register the implementation through the corresponding
META-INF/servicesfile. - A runtime configuration can also select a finder where supported by the JDK.
The selected finder becomes the source of System.Logger instances. The application still calls the same System.getLogger method, so changing the backend does not require changing every logging statement.
Class path and module-path concerns
A provider must be visible to the runtime's service-loading mechanism. A provider that is compiled but absent from the runtime class path or module path cannot be selected.
When using modules, the provider module must declare the service it provides. It must also be available in the runtime module graph. When using the class path, the provider's service registration file must be packaged correctly inside the JAR.
A common failure is accidentally packaging two competing logging systems. For example, an application might include a custom LoggerFinder, SLF4J's platform-logging bridge, and another provider. The resulting behavior can depend on discovery order or provider configuration. Keep one deliberate route for System.Logger.
Routing to SLF4J
SLF4J is another logging facade. If an application already uses SLF4J with Logback, the slf4j-jdk-platform-logging bridge can register a LoggerFinder that forwards System.Logger events into SLF4J. Logback then becomes the configured backend for both APIs.
This arrangement is useful when third-party libraries use System.Logger while application code uses SLF4J. Add the bridge only when that shared route is intended, and verify that the required SLF4J provider is present at runtime.
Exercise
What does System.getLogger depend on to create a logger, and what happens when no custom LoggerFinder is available?
Show answer
It asks the active System.LoggerFinder for a logger. If no custom provider is discovered or configured, the JDK uses its default finder, which normally connects System.Logger to java.util.logging.