Sub topic 1
Why logging matters and what to log
Prerequisites
This chapter assumes you know basic Java syntax and have a simple Spring Boot application built with Maven or Gradle. You should also be comfortable with application.properties or application.yml, and have a basic idea of Spring profiles. Later chapters use these to configure logging.
Why logging matters
A running application is hard to observe. You can't attach a debugger to production, and users rarely report problems with enough detail to reproduce them. Logs are the record your application writes about what it did and what went wrong.
Good logs help you do four things:
- Find the cause of a failure after it happened.
- Follow a request through several components.
- Notice problems early, such as repeated retries or slow calls.
- Audit important actions, such as who changed an order.
Logs are different from metrics and traces. Metrics are numbers over time, such as requests per second. Traces show the path of one request across services. Logs are individual events with a message and context. All three are useful, and this tutorial covers logs.
What to log
Log events that someone will need to understand later. Ask whether the line helps a person diagnose, audit, or monitor the system. If it doesn't, leave it out.
| Log this | Example |
|---|---|
| Application lifecycle | Startup finished, active profile, port |
| Business events | Order placed, payment accepted |
| Failures with context | Order ID, the operation that failed, the exception |
| Calls to other systems | Remote service called, status, duration |
| Security events | Failed login, permission denied |
Each message should say what happened and include the identifiers needed to find the related data. "Order failed" is weak. "Order 8841 failed during payment" is much easier to search for.
What not to log
Some data must stay out of logs, because logs are often copied to shared tools and kept for months.
- Passwords, tokens, API keys, and session IDs.
- Full card numbers and other payment data.
- Personal data such as email addresses, unless you have a clear need and a policy for it.
- Large payloads, such as whole request bodies, on every call.
Also avoid logging inside tight loops. A line written a million times costs time and disk space, and it buries the lines that matter.
How the pieces fit together
The next chapters build on this flow. Your code creates a log event with a level and a message. The logging framework decides whether to keep it, formats it, and sends it to the console or a file.
A first look at the code
Here is the kind of statement you will write throughout this tutorial. It records a business event and includes the order ID. Later chapters explain each part in detail.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service
public class OrderService {
private static final Logger log = LoggerFactory.getLogger(OrderService.class);
public void placeOrder(String orderId, int items) {
log.info("Placing order {} with {} items", orderId, items);
}
}The message names the event and carries the order ID and item count. It contains no secrets.
Exercise
A teammate adds the line log.info("Login for " + email + " with password " + password). Name two problems with it and say what the message should contain instead.
Show answer
It writes a password, which is a secret, and an email address, which is personal data, into logs that many people and tools can read. The message should record the event and a non-sensitive identifier, for example "Login succeeded for user 42" or "Login failed for user 42", and never the password.