Sub topic 2
Choosing Log Levels
This chapter assumes basic Java classes, try/catch, and familiarity with adding Maven or Gradle dependencies. You should also know that a logging backend decides which messages are actually written.
A log level describes the importance of an event. The level helps operators filter normal activity from warnings and failures. Choose a level based on the event's meaning, not on how important the event feels while you are writing the code.
The standard levels
Most Java logging APIs use a similar scale:
| Level | Use it for | Example |
|---|---|---|
| `TRACE` | Very detailed diagnostic information | A cache lookup or method argument |
| `DEBUG` | Information useful during development or investigation | The result of validation |
| `INFO` | Normal, meaningful application progress | A service started or an order was created |
| `WARNING` or `WARN` | An unusual condition that did not stop the operation | A retry or fallback |
| `ERROR` | A failure that needs attention | A request or job could not complete |
TRACE is more detailed than DEBUG. INFO should describe important normal behavior without recording every method call. A warning usually means the application recovered or can continue. An error means an operation failed, although the whole application might still be running.
Do not use ERROR for every exception. An expected invalid input may be a warning or an informational event. Use ERROR when the failure matters to an operator or requires investigation.
System.Logger levels
System.Logger has the levels ALL, TRACE, DEBUG, INFO, WARNING, ERROR, and OFF. ALL enables every level, while OFF disables logging. Applications normally use the middle levels.
The JDK uses WARNING, not WARN. This difference matters when you configure or migrate logging code.
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
public class ImportJob {
private static final Logger LOG =
System.getLogger(ImportJob.class.getName());
public static void main(String[] args) {
String fileName = args.length == 0 ? "orders.csv" : args[0];
LOG.log(Level.INFO, "Starting import of {0}", fileName);
if (LOG.isLoggable(Level.DEBUG)) {
LOG.log(Level.DEBUG, "Import options: validation enabled");
}
boolean recovered = true;
if (recovered) {
LOG.log(Level.WARNING, "The primary source was unavailable; using a backup");
}
LOG.log(Level.INFO, "Import finished");
}
}isLoggable is useful when preparing a debug message requires expensive work. Do not use it for simple messages unless it makes the code clearer.
SLF4J level names
SLF4J provides the same main levels but calls the warning level WARN:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ImportService {
private static final Logger log =
LoggerFactory.getLogger(ImportService.class);
public void run(String fileName) {
log.info("Starting import of {}", fileName);
log.debug("Validation is enabled");
log.warn("The primary source was unavailable; using a backup");
log.info("Import finished");
}
}SLF4J is a facade. A facade is an API that lets application code use logging without depending directly on a particular backend. A backend, such as Logback or the JDK logging system, receives and formats the events.
Set thresholds by environment
A logging threshold determines the lowest level that is enabled. If the threshold is INFO, INFO, WARN, and ERROR events are normally recorded, while DEBUG and TRACE events are ignored. Development environments often enable DEBUG for selected packages. Production usually keeps the root threshold at INFO and enables more detail only for a package under investigation.
Keep levels consistent across a service. If a routine retry is logged as an error in one class and a warning in another, alerts and dashboards become difficult to interpret.
Exercise
An operation retries twice and then succeeds. Which level is usually appropriate for the retry message, and which level is appropriate if all retries fail?
Show answer
Use WARNING or WARN for a retry when the application recovers. Use ERROR when the operation ultimately fails and needs attention.