Sub topic 1
Why Log, and Why Use a Facade
Applications need a record of important events while they run. A log is a time-ordered stream of messages about those events. Logs help you diagnose failures, understand user actions, and check how a system behaves in production.
This chapter assumes basic Java classes and exceptions, familiarity with try/catch, and the ability to add Maven or Gradle dependencies. Knowledge of the Java module system is helpful but not required.
Prerequisites
You should be able to:
- Write and run a basic Java class.
- Call methods and pass arguments.
- Catch an exception with
tryandcatch. - Add a library dependency to a Java project.
Why applications log
A debugger is useful while developing, but you cannot attach one to every production process. A log records selected facts that you can inspect later.
Useful log events include:
- A service starting or stopping.
- A request being accepted.
- A payment being rejected.
- A retry or timeout.
- An unexpected exception.
- A configuration problem.
A log message should explain what happened and include enough context to investigate it. For example, “payment failed” is less useful than “payment failed for order 1842,” provided that the order number is safe to record.
Logging is different from printing directly to standard output. A logging system can filter messages by level, add timestamps, include exception stack traces, write to files, and send output to another system.
What a logging facade does
A logging facade is an API that your application calls without choosing the final logging implementation. The facade defines operations such as “write an informational message” or “record an error.” A separate component, called a backend or provider, decides where and how to write that message.
This separation matters because an application may run in different environments. A local program might write to the console. A server might write JSON records to a collector. The application code can stay the same while the backend changes.
Two common Java facades are:
System.Logger, built into the JDK since Java 9.- SLF4J, a popular library API with support for several backends.
Common backends include java.util.logging, Logback, and Log4j 2. The facade and backend have different responsibilities.
| Component | Responsibility | Example |
|---|---|---|
| Facade | Provides the API called by application code | `System.Logger`, SLF4J |
| Backend | Formats and writes log events | Logback |
| Configuration | Selects levels, destinations, and patterns | `logback.xml` |
The relationship between the parts
Using a facade avoids coupling application code to a backend. It also gives libraries a way to emit logs without forcing every application to use the same output format.
A first `System.Logger` example
System.Logger is available without adding a dependency. The following class logs a normal event and an error:
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
public class Startup {
private static final Logger LOG =
System.getLogger(Startup.class.getName());
public static void main(String[] args) {
LOG.log(Level.INFO, "Application started");
try {
loadConfiguration();
} catch (IllegalStateException e) {
LOG.log(Level.ERROR, "Configuration could not be loaded", e);
}
}
private static void loadConfiguration() {
throw new IllegalStateException("Missing configuration");
}
}The call to System.getLogger obtains a logger associated with the class name. The Level.INFO event describes normal progress. The Level.ERROR event records a failure. Passing e separately preserves the exception and its stack trace.
The application does not decide in this code whether the messages go to the console or a file. The JDK selects a System.Logger backend, and that backend controls the output.
Why not use a backend directly?
Calling a backend directly can be reasonable for a small program, but it creates a dependency throughout the codebase. Replacing that backend later may require changing many classes.
A facade gives you:
- A stable API for application and library code.
- Central control over filtering and output.
- The option to change backends through dependencies and configuration.
- A common approach for code that runs in different environments.
The facade does not solve every problem. You still need to choose useful levels, avoid sensitive data, configure a provider, and ensure that expensive message construction does not happen unnecessarily.
Choosing between the two facades
Use System.Logger when you want a JDK-only API or when a dependency-free library should emit platform logging. Use SLF4J when your project already uses its ecosystem, needs broad backend support, or wants features such as its fluent API and mapped diagnostic context.
The choice of facade does not determine the final destination. In later chapters, you will see how each facade connects to a backend and how applications can use one consistent configuration.
Exercise
Why can changing a logging backend be easier when application code uses a facade?
Show answer
Application classes depend on the facade API instead of backend-specific classes, so the backend can often be replaced through dependencies and configuration.