Scrivio Interview library
DesignAdvanced8 min read · updated 12 November 2026

Personal data appears in logs across many services and breaks your privacy policy. What would you put in place?

We fix this in layers. First we write down what must never be logged (credentials, tokens and personal data), then we change the code so it logs identifiers instead of whole objects and never logs request bodies, and we add masking in the logging configuration only as a safety net. Around that we put automatic scanning of log samples for emails, card numbers and similar patterns, and strict retention and access controls on the log store, so a leak that still happens is found quickly and lives briefly.

Why this question is asked

Logs are copied everywhere: to the log platform, to backups, to support tickets, to a laptop during an incident. Once an email address or a card number is in a log line, it has escaped every control we built around the database, and a privacy policy or a regulation such as GDPR is broken without anyone noticing. Interviewers ask this question at a senior level because the tempting answer, a regular expression that masks everything, is the weakest part of a real fix. They want to see a layered approach that changes how teams write logs. In this answer, we will see how personal data gets into logs, define the rule of what never goes there, change the code to log identifiers, add masking as a safety net, detect leaks automatically, and limit the damage with retention and access controls.

1How Personal Data Ends Up in Logs

Almost nobody writes log.info("customer email is {}", email) on purpose. Personal data usually arrives by accident, through a few habits that look harmless when we write them.

The most common one is logging a whole object. A line such as log.info("Saving {}", customer) calls toString() on the Customer, and a Lombok @Data class or a record prints every field, including name, email, phone and date of birth. The second is logging request or response bodies for debugging, often through a filter that someone added during an incident and never removed. The third is exceptions: a validation message such as "invalid email jane@example.com" or a SQL error that contains the bound parameter values. The fourth is HTTP client and framework debug logging, which can print headers including Authorization tokens.

Notice that all four are structural. No masking rule written after the fact can know every field name in 50 services, so the fix has to start where the data enters the log line.

2Rule One: Define What Must Never Be Logged

We start with a short written policy, agreed with the privacy and security people, that names the categories which must never appear in any log: secrets (passwords, API keys, session cookies, bearer tokens, private keys), payment data (card numbers, CVV codes, bank account numbers) and personal data (names, emails, phone numbers, addresses, government ids, health data, precise location). It also says what is allowed instead: internal ids such as customerId=84213, order ids, request ids, counts, and coarse values such as a country code.

This short list matters more than it seems. It turns a vague worry into a rule that a code reviewer can apply in ten seconds, that a scanning tool can test, and that we can point to when a team asks whether logging a username is fine.

What may and may not go into a log line
CategoryExampleIn logs?
Secretpassword, bearer token, API keynever
Payment datacard number, CVV, IBANnever
Personal dataemail, phone, name, addressnever
Internal identifiercustomerId=84213, orderId=A-1009yes
Technical contexttraceId, status=422, durationMs=38yes
Coarse attributecountry=DE, plan=premiumyes, if the policy allows
The policy is short enough for a reviewer to apply during a pull request.

3Rule Two: Log Identifiers, Not Objects

The main control is in the code. We log the id of the thing, plus the few technical facts that help debugging, and we never pass a whole domain object or a request body to the logger. Here is the habit we remove and what replaces it.

Java
// Before: toString() of a record prints every field
record Customer(long id, String name, String email, String phone) {}
log.info("Updating {}", customer);
// Updating Customer[id=84213, name=Jane Roe, email=jane@example.com, phone=+49 151 ...]
// After: only the identifier and technical context
log.info("Updating customer customerId={} fields={}", customer.id(), changedFieldNames);
// Updating customer customerId=84213 fields=[email, phone]
// Sensitive types can also refuse to print themselves
record Email(String value) {
@Override public String toString() { return "Email[***]"; }
}

Notice that the second line still tells an engineer what happened: which customer and which fields changed. Support can look up the customer by id in a system that has proper access control, so nothing useful is lost.

For types that hold personal data, overriding toString() to print a placeholder protects us from the accidental cases, because even a careless log.info("{}", email) prints Email[***]. We also remove request and response body logging from filters, and we keep framework loggers such as the HTTP client wire logs at WARN in every environment except a developer's machine. Exception messages need the same care: we write validation messages that name the field ("email is invalid") rather than echo its value.

4Masking in the Logging Configuration as a Safety Net

Next we add masking at the logging layer, for example a Logback converter or a structured logging customizer that replaces anything that looks like an email or a long run of digits before the line is written. This catches mistakes that slipped through review.

Java
// A Logback converter that masks emails and card-like numbers in the message
public class MaskingConverter extends ClassicConverter {
private static final Pattern EMAIL = Pattern.compile("[\\w.+-]+@[\\w-]+\\.[\\w.]+");
private static final Pattern CARD = Pattern.compile("\\b\\d{13,19}\\b");
@Override
public String convert(ILoggingEvent event) {
String m = event.getFormattedMessage();
m = EMAIL.matcher(m).replaceAll("***@***");
return CARD.matcher(m).replaceAll("****");
}
}
// "refund for jane@example.com card 4111111111111111" -> "refund for ***@*** card ****"

But there is a catch, and it is the reason masking is only the safety net. Patterns cannot recognise a name, an address or a date of birth, they miss emails split across fields or encoded in JSON, and they cost CPU on every single log line. They also only cover the message: structured fields, MDC values and exception stack traces need their own handling. So we tell teams clearly that masking exists to catch the rare slip, and that the rule from the previous section is what actually keeps data out.

5Detecting Leaks Automatically

Rules and masking reduce leaks, but we also need to know when one happens. We run a scheduled job that takes a sample of recent log lines from each service, for example 10,000 lines per service per hour, and scans them for the same patterns: emails, card numbers that pass the Luhn check, phone numbers, JWTs that start with eyJ, and long hex strings that look like keys. A finding opens a ticket for the owning team with the logger name and a redacted example.

The point is to measure the problem: after a month we know which services leak and whether the number is falling.

Step throughLayers that keep personal data out of logs
  1. Written policywhat must never be logged
  2. Code review and typeslog ids, safe toString()
  3. Masking in the log pipelinesafety net for slips
  4. Scan log samplesfind what still leaks
  5. Retention and accesslimit the damage

5 steps in order. Play them, or step through them one at a time.

Each layer catches what the one above it missed.

6Retention and Access Controls on Log Storage

The last layer limits the damage of anything that still gets through. Logs are personal data stores in the eyes of a regulator, so we treat them like one. We set retention by purpose: for example 14 days for application logs, 30 days for warnings and errors, and a longer, separately approved period only for audit logs, which are designed from the start to contain no more personal data than they need.

Access follows the same thinking. Engineers can read logs for the services they own, production access is logged, and exports are restricted. Finally, we report progress to leadership as numbers: services scanned, findings per week, and time to fix, so the work does not quietly stop after the first push.

7Follow-up questions

Is logging a user id itself personal data?

Often, yes, in the legal sense, because it can be linked back to a person. But it is pseudonymous: on its own it reveals nothing, and only systems with proper access control can resolve it. Most privacy policies accept internal ids in logs with limited retention, while they do not accept names or emails. We confirm this with the privacy team and write the answer into the policy so teams do not each decide differently.

Why not just mask everything with one big regular expression?

Because it gives a false sense of safety. Patterns cannot recognise names or addresses, they miss data inside JSON or encoded strings, and running many expressions on every line costs real CPU in a busy service. Masking is useful as a last line of defence, but the main control has to be that code never hands personal data to the logger in the first place.

What do we do with logs that already contain personal data?

We treat it as an incident. We find the time range and services affected, delete or redact those indexes in the log platform, check backups and exports, fix the code that produced the lines, and add a scan rule or a test so the same leak is caught next time. If the policy or the law requires it, the privacy team decides whether the leak must be reported.

8Conclusion

In this answer, we saw that personal data reaches logs through habits such as logging whole objects, request bodies and exception messages. We fixed it in layers: a short policy of what must never be logged, code that logs identifiers and types whose toString() hides sensitive values, masking in the logging configuration as a safety net rather than the main control, automatic scanning of log samples to find what still leaks, and retention and access controls so that anything that does leak lives briefly and is seen by few people.

What to say in the interviewAbout 90 seconds aloud

Open with

I would treat this as a layered problem rather than a regex problem. First I would agree a one-page policy with the privacy team: credentials, tokens, payment data and personal data such as names, emails and phone numbers are never logged, and internal ids, trace ids and technical context are fine.

Then, if they want more

Then the main control is in the code: we log ids instead of whole objects, we remove request and response body logging, we keep wire logging off in production, and for sensitive types such as Email we override toString so even a careless log statement prints a placeholder.

Third, I would add masking in the logging configuration, for example a Logback converter for emails and card numbers, but I would be clear that it is only a safety net, because patterns cannot recognise a name or an address and they cost CPU on every line.

Fourth, I would scan samples of production logs automatically, say every hour per service, for emails, Luhn-valid card numbers and tokens, and open tickets for the owning team, so we can measure whether leaks are falling.

Finally, I would treat the log store as a personal data store: short retention by purpose, around two weeks for application logs, access limited to service owners, audited access, and a way to delete affected ranges when a leak is found.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Boot reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Personal data appears in logs across many services and breaks your privacy policy. What...": https://scrivio-interview-library.netlify.app/spring-boot/boot-logging/fca374badb/