Sub topic 1
Logging in Java with System.Logger and SLF4J
Prerequisites
This chapter assumes basic Java classes, try/catch, and adding Maven or Gradle dependencies. Familiarity with logging libraries is helpful but not required.
Why log, and why use a facade
Logging records events while an application runs. Logs help diagnose failures, understand important state changes, and measure behavior in production. A log message usually has a level, message text, timestamp, and logger name.
A logging backend writes or routes messages. Examples include java.util.logging and Logback. A logging facade is a stable API that application code calls without depending directly on one backend. This separation lets you change output format, destinations, or filtering without rewriting application classes.
Java provides the System.Logger facade in the JDK. SLF4J is another widely used facade. Libraries commonly use a facade so the application can choose the backend and centralize its logging policy.
Choosing log levels
Choose a level according to the operational value of an event.
| Level | Typical use |
|---|---|
| `TRACE` or `DEBUG` | Detailed diagnostic information |
| `INFO` | Normal application milestones |
| `WARNING` | Unexpected condition that the application handled |
| `ERROR` | Operation failed or needs attention |
Avoid logging routine details at INFO when they could overwhelm production logs. Never put passwords, tokens, or complete payment-card data in a message.
Logging with `System.Logger`
System.Logger is available from Java 9. Its formatting uses numbered placeholders such as {0}. A supplier can delay message construction until the level is enabled.
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
public class OrderService {
private static final Logger LOG =
System.getLogger(OrderService.class.getName());
public static void main(String[] args) {
String orderId = args.length == 0 ? "" : args[0];
LOG.log(Level.INFO, "Placing order {0}", orderId);
if (LOG.isLoggable(Level.DEBUG)) {
LOG.log(Level.DEBUG, () -> "Order length: " + orderId.length());
}
try {
if (orderId.isBlank()) {
throw new IllegalArgumentException("Empty order id");
}
} catch (IllegalArgumentException exception) {
LOG.log(Level.ERROR, "Could not place order", exception);
}
}
}Passing the exception separately records its stack trace. isLoggable is useful when preparing a message requires expensive work.
How `System.Logger` finds its backend
When code requests a System.Logger, the JDK asks a LoggerFinder for an implementation. The default finder routes messages through the JDK logging system, usually java.util.logging. An application or library can provide another finder through Java’s service-provider mechanism.
This design keeps the application API separate from the output system. A provider must be selected early and consistently, especially in modular applications.
Exercise
Why might an application use System.Logger instead of calling java.util.logging directly?
Show answer
It keeps application code independent of a specific backend, so the logging implementation can be replaced or configured centrally.