Empower growth and innovation with the latest Program Dev insights

The ticket only has the user's phone number, but the logs show nothing but asterisks — how can on-call engineers still trace the issue?

Oct 9, 2026 Read: 23

Masking phone numbers as asterisks in logs is usually not something the logging library alone can solve; the problem mostly arises at the step where data turns from a request body into a log string. Under common 2026 delivery practice, the main masking effort should sit between parameter binding and object serialization, with the log component's output side acting only as a fallback; at the same time, keep a non-reversible correlation key so on-call colleagues can still trace back to the same person. Blocking only the output side leaves the risk to string concatenation and exception stack traces that bypass the component.

Plaintext phone numbers in logs: the trouble is not just being seen

Logs are not read by just one person. The common exposure surface of a project's logs includes developers, operations, outsourced inspectors, centralized log platforms and other roles, with a typical retention range of 30 to 180 days, and longer in some industries under compliance requirements. A plaintext phone number produced by one API call spreads as copies to these places, while most of those roles do not have business permission to access that field.

What is more subtle is that the logs themselves are the basis for troubleshooting. If masking is done as full masking, locating an issue by number in production breaks the chain, and engineers can only rely on timestamps and luck. Qualified log masking should satisfy two things at once: it looks non-reversible from the outside, while internal troubleshooting can still trace back to the same person.

  • External risk: log file permissions, the collection pipeline, backup archives — any link can be read by unauthorized people.
  • Internal risk: troubleshooting capability is inadvertently damaged; overly aggressive masking actually slows down locating issues.
  • Time risk: one oversight left on disk means later remediation has to touch existing files, with costs measured in weeks.

Which fields need masking: look at identifiability first, then troubleshooting dependency

Whether a field needs masking should not be decided by a gut feeling of sensitive or not sensitive, but by whether it can point to a specific person and whether the troubleshooting chain depends on it.

  1. First, can it identify a person on its own? Phone numbers, ID card numbers, passport numbers, bank card numbers, email addresses, precise addresses, device unique IDs — appearing alone they can point to a specific individual, so mask by default.
  2. Second, can it identify someone in combination? Date of birth, gender, postal code and company name are each not sensitive, but several fields together can be reverse-engineered to a specific person, so when they appear as a group they should be handled together.
  3. Third, is it used as a query key? Phone numbers, user identifiers and order numbers are often used as search conditions by operations or on-call colleagues; before masking, confirm whether the troubleshooting chain depends on it — if so, keep a hash column or the trailing digits.
  4. Check the platform specification and acceptance checklist: follow the log specification of your platform or the client, and do not close the issue based only on your own judgment.

The baseline for judging whether it is acceptable is: given a masked log, a stranger cannot reconstruct the full number, while an on-call colleague can locate the same request within a reasonable time. The core tension in masking is between usability and visibility; judge identifiability first, then troubleshooting needs, so you are less likely to blindly mask everything and then have to redo the work when production issues cannot be investigated.

Where should masking mainly intercept: four layers of defense

Treating masking as a single action placed somewhere usually springs a leak a few months later, because new APIs and third-party components do not automatically follow old conventions. A more stable approach is to arrange it in four layers, with the first two controlling most output and the last two as fallbacks.

  1. Entry/reception layer: after object binding and before entering business logic, use field annotations or validators to turn fields that do not need plaintext into masks or hashes; for scenarios such as sending SMS, read the plaintext in memory first, then mask the record.
  2. Object serialization layer: uniformly use the framework's serialization capability, and prohibit hand-written string concatenation for logging; a customized serializer outputs masks and can also control objects in object printing, JSON output and exception messages.
  3. Log output layer: do regex fallback in the formatting stage of the log component to cover output outside business code. Its coverage is limited, and regex easily hits long numeric strings such as order numbers by mistake, so it is only suitable as a second safety net.
  4. Storage and retrieval layer: run rules again when the log platform collects logs, index only masked values, and tighten log file permissions and retention periods so the life cycle of copies is controllable.

The four layers do not need to be fully filled in at every layer. If the first two are done well, the last two only need to act as fallbacks; doing only the last two amounts to assuming all business code will follow the rules long term, which is hard to sustain in projects with multiple collaborators and staff turnover. During acceptance, randomly sample three to five logs containing sensitive fields and check each one to confirm whether any recoverable plaintext remains.

Common pitfalls in projects

  • Blocking only the logging component: objects printed directly in business code, request bodies in exception stack traces, and messages logged by third-party libraries themselves will all bypass the component and write plaintext into files.
  • Treating a mask as encryption: a mask is irreversible and not protected by a key; if compliance requires encryptable storage and controlled decryption, masking cannot replace encryption.
  • Writing separate rule sets: API A keeps the first three digits and API B keeps the last four, so they do not match during troubleshooting and people are unsure which log is trustworthy.
  • Leaving no correlation key after masking: everything is masked and there is no request trace identifier, so when a failure occurs you can only rely on timestamps and luck.

In one case, a project had a tight budget and about a two-week schedule, so developers took the quick route and logged directly via string concatenation. After launch, the client's compliance self-inspection raised remediation. The constraints were that historical logs were already on disk and the service could not be stopped and rerun. The approach was to pull the rules back to the serialization layer, add request trace IDs for correlation, and separately clean logs still within the retention period. It cost about 3 to 5 person-days extra overall, a typical range of saving ten minutes first and making up for it over several days later.

How to weigh the different approaches

Masking is the same task, but where it is implemented makes a clear difference in remediation cost and leak risk. Below is a side-by-side comparison by experience range, to help you choose based on project scale and team size.

  • Global serializer or field annotations: configured once and effective project-wide, covering object printing and common exception output; adding or removing fields requires maintaining annotations in sync. Remediation effort has an experience range of 1 to 3 person-days, suitable for projects with many APIs and multiple collaborators.
  • Manual masking before concatenating logs: change whichever API and line of code, flexible but highly dependent on people — one missed spot is one missed spot. Suitable for early-stage projects with few APIs, with a typical cost range of 0.5 to 2 person-days, but maintenance cost grows with the number of APIs.
  • Regex fallback in the logging component: can go live without changing business code, but easily hits long numeric strings such as order numbers by mistake, and cannot cover output that bypasses the component. Suitable as a supplementary measure, not as the only measure.
  • Filtering on the log platform side: zero intrusion into the business, suitable for quick compliance in existing systems; but plaintext has already entered the collection pipeline, and it may not be recognized under strict auditing.

The judgment criterion can be simplified to one sentence: if more than three people on the team are writing logs at the same time, do not bet consistency on personal habits; prefer a global solution, backed by checklist-based spot checks.

Applicable and non-applicable boundaries

Log masking fits these situations: logs are written to disk or enter a centralized platform, multiple roles can read them, fields such as phone numbers and ID numbers can directly identify individuals, and you need to handle platform or client compliance self-inspections. In such scenarios, masking is part of the infrastructure, not an optional decoration.

Conversely, some situations do not need a heavy solution: pure intranet single-machine tools, logs output only to the local console and not written to disk, fields already encrypted before storage and logs only printing hash values — here adding another layer of masking has limited benefit and instead increases maintenance. The boundary can be remembered as one sentence: as long as logs leave the machine that produced them, masking is worth making the default behavior; for local-only, clear-as-you-go scenarios, start with team specification constraints and do not immediately change the architecture.

Common questions

Are log masking and database encryption the same thing?

No. Encryption is oriented toward storage and controlled decryption, mainly to prevent database dumping; log masking is oriented toward display and is usually non-reversible. They protect different positions and need to be configured separately; one cannot replace the other.

If only the last four digits of a phone number are kept, can it still be identified?

It is possible. The last four digits combined with time, region and order information still carry re-identification risk. For logs shared externally or retained long term, you can keep only the prefix and one trailing digit, or switch to a hash value for correlation.

How do you manage logs written by a third-party SDK itself?

First check whether it provides a masking or disable switch; turn it off if possible. If it cannot be turned off, raise its log level, or redirect its output to a separate file and shorten retention, to avoid mixing with production logs.

After masking, what if production cannot locate the user?

Keep a non-sensitive correlation key, such as a hash of the user identifier or a request trace ID. Use it to locate first during troubleshooting, then have someone with permission look up the plaintext in a controlled environment, so you do not need to put plaintext back into the logs.


If you are about to push log masking forward, it is advisable to first inventory the log exits in the project: which roles can view them, how long they are retained, and which fields can identify a person on their own. Start with the most strongly identifying fields, put the rules in the serialization layer, and use the logging component as a fallback. If logs are only for local debugging and are not retained, just set team specifications first — there is no need to undertake a major overhaul immediately.

Have a similar project in mind?
Contact us for a one-to-one project reference proposal
Obtain Proposal
Are you ready?
Then reach out to us!
+86-13370032918
Discover more services, feel free to contact us anytime.
Please fill in your requirements
What services would you like us to provide for you?
Your Budget
ct.
Our WeChat
Professional technical solutions
Phone
+86-13370032918 (Manager Jin)
The phone is busy or unavailable; feel free to add me on WeChat.
E-mail
349077570@qq.com
Submitted successfully
Thank you for your trust. We will contact you soon!
Recommended projects for you