Sub topic 3
Understanding log levels
This chapter assumes you can write a basic Spring Boot application and know that Spring Boot uses SLF4J with Logback by default. You also need to be comfortable editing application.properties.
What a level does
A log level marks how important a message is. It also decides whether the message is written at all. Each logger has a threshold. Messages at or above that threshold are written, and messages below it are dropped.
SLF4J defines five levels, from most to least severe:
| Level | Typical use |
|---|---|
| ERROR | An operation failed and needs attention |
| WARN | Something unexpected happened, but the application continued |
| INFO | Normal milestones, such as startup or a completed order |
| DEBUG | Detail useful when diagnosing a problem |
| TRACE | Very fine detail, such as every step in a loop |
Logback also accepts OFF in configuration, which turns a logger completely off. Spring Boot accepts FATAL as an alias, but Logback maps it to ERROR.
How the threshold works
Setting a logger to INFO means INFO, WARN, and ERROR messages pass. DEBUG and TRACE messages are discarded.
Spring Boot's default root level is INFO. This is why you see startup messages but no DEBUG output unless you ask for it.
Choosing the right level
Pick the level by who needs to see the message and when.
- ERROR: use it when a request or job fails. Someone should be able to act on it. Do not log an ERROR for a user typing a wrong password. That is normal behavior.
- WARN: use it for recoverable problems, such as a retry that succeeded or a deprecated option in use.
- INFO: use it for events that describe what the application is doing. In production you read these to follow the story.
- DEBUG: use it for values and decisions you need while investigating a bug. It is usually off in production.
- TRACE: use it sparingly. Most teams never enable it outside a short debugging session.
Here is a small example that uses several levels.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PaymentClient {
private static final Logger log = LoggerFactory.getLogger(PaymentClient.class);
public boolean charge(String orderId, int attempt) {
log.debug("Charging order {}, attempt {}", orderId, attempt);
if (attempt > 1) {
log.warn("Retrying payment for order {}", orderId);
}
boolean ok = attempt >= 2;
if (ok) {
log.info("Payment accepted for order {}", orderId);
} else {
log.error("Payment rejected for order {}", orderId);
}
return ok;
}
}With the default INFO threshold, the debug line is skipped. The other three can appear, depending on the path taken.
Common mistakes
- Logging routine events at ERROR. Alerts then fire constantly and people learn to ignore them.
- Logging everything at INFO. The important lines get buried.
- Leaving DEBUG on in production. It costs disk space and CPU, and it can expose data that should not be stored.
Exercise
A logger is configured at WARN. Which of these messages are written: log.error, log.warn, log.info, log.debug?
Show answer
Only log.error and log.warn are written. WARN is the threshold, so messages at WARN or more severe pass, and INFO and DEBUG are dropped.
Exercise
A user enters an invalid email address in a sign-up form, and your code handles it by returning a validation message. Which level fits a log line about this, and why?
Show answer
INFO or DEBUG fits, depending on how useful the line is. The application worked as designed, so it is not an ERROR or WARN.