Video in progress
What do you do to keep passwords, tokens or personal data (PII) from ending up in logs?
The main defense is at the code level: don't log whole request and response bodies, control the toString() of DTOs, and wrap secret values in a dedicated type. Extra layers are masking in the log pipeline and restricting who can access the logs.
📖 Answer
Typical leak paths:
log.info("Request: {}", request): the automatictoString()from Lombok's@Dataor from arecordprints the password, card number, passport series;- body logging switched on "for debugging" in an HTTP client or gateway (the
Authorizationheader goes out too); - the exception message:
"User with email ali@mail.uz not found"; - SQL logs with their parameters.
Hiding a field in a record:
public record LoginRequest(String username, String password) {
@Override
public String toString() {
return "LoginRequest[username=" + username + ", password=***]";
}
}
In Lombok it is @ToString.Exclude. But this is a rule everybody has to remember, and it gets forgotten. Wrapping the secret value in a dedicated type is more reliable:
public record Secret(String value) {
@Override public String toString() { return "***"; }
}
A field of type Secret never ends up in a log by accident.
The second layer is masking at the appender level: in Logback, replacing patterns like card numbers or JWTs (eyJ...) with a regex. This is a safety net, not the main defense: a regex doesn't catch everything and it costs CPU.
The third is process: in code review, pay attention to log.*(... request ...) lines, check log output in tests, restrict access to the log system and set a retention period. From the point of view of personal data laws, a passport number in a log is no different from one in the database, and the same protection and deletion requirements apply to it.
Sign up to mark studied questions and save your progress.