In Java 9 and newer, System.getLogger(name) returns a System.Logger, a small logging interface built into the JDK. You log with logger.log(level, message) using the levels TRACE, DEBUG, INFO, WARNING, and ERROR.
System.Logger is only a facade: by default messages go to java.util.logging, which prints INFO and above to System.err with a time stamp. Frameworks can plug in their own backend.
A static method of the java.lang.System class, added in Java 9 (JDK 9). Call it on the class name, as in System.getLogger(...). No import is needed, because classes in java.lang are always visible. Requires Java 9 or newer.
A named logger.getName() gives back the name passed to getLogger. Dotted names let a configuration file target a whole package.
The default threshold. Without a custom configuration, java.util.logging lets INFO and more serious levels through, so INFO and ERROR report true.
Debugging is off. DEBUG is below INFO and therefore disabled. A logging.properties file with com.example.shop.level = FINE would turn it on.
Example 2 — Skip Building Messages Nobody Will See
Building a log message can be expensive. With a Supplier or an isLoggable guard, the work only happens when the level is enabled.
Java
import java.util.function.Supplier;
public class SystemGetLoggerLazy {
static int calls = 0;
static String expensiveReport() {
calls++;
return "report number " + calls;
}
public static void main(String[] args) {
System.Logger logger = System.getLogger("SystemGetLoggerLazy");
Supplier<String> message = SystemGetLoggerLazy::expensiveReport;
logger.log(System.Logger.Level.DEBUG, message);
logger.log(System.Logger.Level.TRACE, message);
System.out.println("Reports built with a Supplier: " + calls);
if (logger.isLoggable(System.Logger.Level.DEBUG)) {
logger.log(System.Logger.Level.DEBUG, expensiveReport());
}
System.out.println("Reports built after the guard: " + calls);
}
}
Terminal output
Requires Java 9 or newer. Shown for the default logging configuration (no custom logging.properties), where the threshold is INFO.
Reports built with a Supplier: 0
Reports built after the guard: 0
How It Works
A message made on demand.log(level, Supplier) receives a method reference instead of a finished string. The logger calls get() only if it will really output the message.
Never called. DEBUG and TRACE are both disabled by default, so the supplier is never asked and the counter calls stays at 0. Nothing is written to System.err either.
The classic guard. Wrapping the call in if (logger.isLoggable(...)) has the same effect for code that builds a plain string, so expensiveReport() is skipped again and the count is still 0.
Example 3 — List Every Level with Its Severity
Name the logger after the class and walk through the five normal levels, showing each severity number and whether it is enabled.
Class name as logger name.SystemGetLoggerLevels.class.getName() is the class's full name; it has no package here, so the logger is simply called "SystemGetLoggerLevels".
Severity numbers. Each Level has a fixed severity from getSeverity(): 400 for TRACE up to 1000 for ERROR. Higher means more serious. The special levels ALL and OFF are left out of the list.
A threshold at 800. Everything from INFO (800) upward is enabled, and TRACE and DEBUG fall below it, matching the default java.util.logging level.
Frequently Asked Questions
A small logging interface added in Java 9. Get one from System.getLogger(name) and log with levels like INFO and ERROR.
With the default backend, java.util.logging prints INFO and higher to System.err, including a time stamp and the source.
The default threshold is INFO. Lower it in a logging.properties file, for example com.example.level = FINE, or configure your logging framework.
No. It was added in Java 9. In Java 8 use java.util.logging.Logger directly.
getLogger(name, bundle) lets you log message keys that are looked up in the bundle, for translated log messages.