Video in progress

What is the problem if a password, card number or personal data ends up in logs, and how do you prevent it?

Logs are open to many people and systems and are kept for a long time, so a secret that lands there has effectively leaked. To prevent it, don't log whole DTOs, mask values and control toString().

📖 Answer

Logs go to Elasticsearch, which support, DevOps and sometimes outside contractors can see. Backups are kept for months. One log.info("Request: {}", request) and card numbers are everywhere. Under PCI DSS and personal data laws, that is a serious incident.

What you do in practice:

public record LoginRequest(String phone, String password) {
    @Override
    public String toString() {
        return "LoginRequest[phone=" + mask(phone) + ", password=***]";
    }
}
  • don't log request/response bodies as a whole, write the fields you need separately;
  • card: only the last 4 digits; phone: +99890***4567;
  • never write a token, the Authorization header or a cookie;
  • exclude secret fields with Lombok's @ToString.Exclude;
  • extra masking in the logging configuration (with a regex) as the last layer of defense.

In code review, if you see a log.*(... request) line, stop and check: most leaks come from exactly such lines.