PII in logs: masking and sampling without losing errors
Logs exist to investigate incidents. The more context they carry, the easier it is to find the cause — and the higher the risk that an email, a phone number, or a card number ends up in a system where it does not belong.
Where PII appears
It gets into the log not only through an explicit log.info(email). Typical
paths:
- the request and response body in full;
- a user object from the CRM in
context; - an exception message with request parameters;
- SQL parameters in a debug log;
- the model prompt and response, if they are logged for debugging;
- nested structures:
context.user.email,payment.pan,contacts[].
So masking only the message field is not enough. You need a recursive walk
over every string value in dicts and lists.
Masking formats
A mask must stay useful for debugging while removing the identifier:
| Data | Before | After |
|---|---|---|
| email | ann.lee@example.com | a***@example.com |
| phone | +7 (999) 123-45-67 | ***4567 |
| card | 1234 5678 9012 3456 | **** **** **** 3456 |
The first character of the email and the last four digits of the phone/card let you correlate records with each other without revealing the data in full. The domain is kept so you can tell a corporate address from an external one.
The order of substitutions matters: card first (16 digits), then phone (10–11), then email. Otherwise the phone mask can "eat" part of the card number.
Sampling: why and how
Storing every INFO log is expensive. Sampling reduces the volume but must be
deterministic: the same operation on a re-run must land in the log (or not) the
same way. Otherwise correlation breaks and tests become flaky.
A simple option is a hash of request_id:
bucket = int.from_bytes(
hashlib.sha256(request_id.encode("utf-8")).digest()[:4], "big"
) / 2**32
keep = bucket < sample_rate
sample_rate=0 keeps only errors, 1 keeps everything. The strict < makes
the boundaries predictable.
random.random() does not work here: the decision changes from run to run.
Errors are never sampled
ERROR is the signal the log exists for. If you thin out errors along with
regular records, an incident may not make it into the sample, and metrics and
alerts start lying. So:
- check the level first;
- always keep
ERROR; - only then apply sampling to the rest;
- mask every record that is kept.
For ERROR, request_id may be missing: the error matters more than the
correlation requirement. For regular records, request_id is mandatory,
otherwise the sampling decision is not reproducible.
Retention and access
Masking in the application is not the only protection. You also need:
- a short retention period for raw logs;
- restricted access to them;
- a ban on pasting PII into shared chats or tickets;
- scheduled scanning of logs for emails/phones/cards;
- separate audit storage if the law requires PII to be kept.
If PII does get into a log, that is an incident: assess the scope, restrict access, delete the data according to policy, and notify the owners.
What to test
ERRORis kept atsample_rate=0;sample_rate=1keeps every level;- for a mid value, the set of records matches an independently computed hash;
- the same
request_idproduces the same decision; - email, phone, and card are masked in
messageand in nested fields; - the input object is not mutated;
- an empty or invalid
sample_rate, a missinglevelorrequest_idproduce a clear error.
Common mistakes
- Masking only
messageand leavingcontextunmasked. - Swapping the phone and card order.
- Using
randominstead of a stable hash. - Thinning out errors "while we're at it".
- Logging the whole prompt or model response without a filter.
- Keeping a log with PII "just in case" with no retention and no role-based access.