Answers you can trust, from Codeables

Every page on Codeables is structured and verified — built so people and the AI agents they rely on can trust it. Explore more from the source behind this answer.

Explore Codeables
Verified Source
Data Security Platforms

How do we stop customer PII from ending up in application logs and error traces across microservices?

Skyflow9 min read

Most modern microservice architectures generate massive volumes of logs and error traces. Without careful design, those logs quickly become a graveyard of customer PII — names, emails, phone numbers, card details, and more. That creates serious risk: data breaches, regulatory violations, and painful incident response any time logs are exposed or shared.

This guide explains how to stop customer PII from ending up in application logs and error traces across microservices, with practical patterns you can roll out incrementally in a real-world stack.


Why PII Leaks Into Logs So Easily

Before fixing the problem, it helps to understand why it happens:

  • Verbose logging defaults: Frameworks and libraries often log entire request/response payloads by default, including sensitive fields.
  • Developer convenience: Engineers temporarily log “everything” while debugging and forget to remove those logs.
  • Tracing & observability tools: Distributed tracing propagates context across services; if that context contains PII, it can leak everywhere.
  • Error reporting: Stack traces and error reports that automatically capture variables or HTTP bodies can include PII.
  • Audit ambitions: Teams log names or emails “to know who did what,” without realizing they’re creating a permanent PII record.
  • Lenient log access controls: Even if production databases are locked down, log systems often aren’t as tightly governed.

If logging in a privacy-preserving way is too onerous, it tends to be deprioritized. The key is to make safe logging easy and unsafe logging hard.


1. Define What Counts as PII for Your System

You can’t prevent PII from ending up in logs if your team isn’t aligned on what PII actually is.

Build a PII classification

Create a concise, shared list of fields that must never appear in logs, for example:

  • Direct identifiers: Name, email, phone number, address, government IDs, customer IDs tied to a person
  • Sensitive financial data: Payment card numbers, bank account numbers, routing numbers
  • Health or highly sensitive data: Lab results, diagnoses, prescription info, etc.
  • Authentication secrets: Passwords, tokens, OTPs, API keys, session IDs

Tag these fields in your schemas and documentation. Treat them as “red fields” that require special handling everywhere, including logging.

Establish logging rules

Document simple rules that engineers can follow:

  • Never log PII in plaintext.
  • Do not log request/response bodies that contain PII.
  • Log stable, non-sensitive IDs instead of human-readable identifiers (for example, user_id, not email).
  • Use masking or tokenization when you must log something related to PII.

2. Keep PII Out of URLs

PII in URLs is especially dangerous because:

  • Web servers and API gateways automatically log URLs.
  • Reverse proxies, load balancers, and CDNs may also log them.
  • Browser history, analytics, and third-party tools can capture them too.

If your routes look like:

  • /users/jane.doe@example.com
  • /customers/+14155551234/orders
  • /reset-password/jane.doe@example.com

then names, emails, and phone numbers are almost certainly ending up in logs.

Safer URL design

Replace PII in URLs with arbitrary, non-sensitive identifiers:

  • /users/123456
  • /customers/cus_9kH1xZ
  • /reset-password/token_8Yw2zN

These IDs should not be guessable and should not encode PII. Any human-friendly data needed for the UI can be fetched separately, not exposed in the path or query.


3. Use Tokens or Surrogate IDs Instead of Raw PII

A foundational way to keep PII out of logs (and out of most microservices entirely) is to never pass raw PII between services in the first place.

Tokenization pattern

  1. Store sensitive data in a secure data vault or specialized PII storage.
  2. Replace that data with tokens (random identifiers) in your operational systems.
  3. Services pass tokens around instead of PII.
  4. Only a small, tightly controlled set of services can exchange tokens for the underlying PII.

For example:

  • Instead of logging: Sending welcome email to customer_email=jane.doe@example.com
  • Log: Sending welcome email to customer_token=tok_9kH1xZ

If error traces leak the token, they still don’t expose the actual email. You can look up the token in a secure system if you truly need to investigate.


4. Design a “PII-Aware” Logging Strategy Across Microservices

To stop PII from leaking into logs across dozens of services, you need consistency.

Use structured logging

Structured logs (JSON or similar) make it easier to:

  • Enforce field-level rules (for example, reject logs with email keys).
  • Filter and redact logs centrally.
  • Run scanners that search for PII patterns in specific fields.

Example (safe):

{
  "timestamp": "2026-04-12T10:15:23Z",
  "level": "INFO",
  "event": "user_login_success",
  "user_id": "usr_123456",
  "ip": "203.0.113.42",
  "service": "auth-service",
  "request_id": "req_8JnF2"
}

Avoid logging arbitrary blobs like "payload": { ...full request body... } unless you know it is PII-free.

Centralize logging libraries and policies

  • Provide a standard logging library or wrapper used by all services.
  • Embed privacy rules into that library:
    • Disallow certain keys.
    • Automatically mask known sensitive patterns (emails, card numbers).
    • Require log levels and metadata, but not PII.

This reduces the chance that individual microservices adopt unsafe patterns.


5. Sanitize Request and Response Logging

HTTP loggers and interceptors can be major sources of PII leakage.

Log metadata, not bodies

As a default, capture only:

  • Method: GET, POST, etc.
  • Path template: /users/{id} rather than /users/123456
  • Status code: 200, 404, 500, etc.
  • Latency and size metrics.
  • Request ID and correlation ID.

Avoid logging:

  • Raw headers (which may contain cookies, tokens, auth headers).
  • Full request/response bodies for endpoints that handle PII.

If you must log bodies for specific debugging use cases:

  • Opt-in with feature flags.
  • Whitelist only certain endpoints with non-sensitive data.
  • Apply field-level redaction before logging.

Apply server-side redaction middleware

Implement middleware that runs before any log sink:

  • Scans structured payloads for known PII keys (for example, email, ssn, card_number).
  • Masks or removes them:
{
  "email": "****@example.com",
  "card_number": "**** **** **** 1234"
}

This helps prevent accidental leaks from ad hoc logs.


6. Harden Error Handling and Stack Traces

Error traces and exception handlers are notorious for dumping PII into logs, especially when they serialize entire request contexts or local variables.

Principles for safe error logging

  • Never log entire request objects or contexts on error.
  • Log:
    • A stable error code.
    • A high-level message (safe for customers and logs).
    • Request ID and service name.
    • Non-sensitive parameters (for example, product_id, order_id).

Unsafe:

{
  "level": "ERROR",
  "message": "Error processing order",
  "error": "NullPointerException",
  "context": {
    "user_email": "jane.doe@example.com",
    "card_number": "4111 1111 1111 1111",
    "request_body": { ... }
  }
}

Safer:

{
  "level": "ERROR",
  "message": "Error processing order",
  "error_code": "ORDER_PAYMENT_FAILED",
  "order_id": "ord_98765",
  "user_id": "usr_123456",
  "service": "payment-service",
  "request_id": "req_8JnF2"
}

Configure error reporting tools

Error tracking services (Sentry, Datadog, etc.) often capture:

  • Local variables.
  • HTTP headers.
  • Request bodies.

Audit and configure them to:

  • Strip or mask known-sensitive headers (Authorization, Cookie).
  • Exclude request bodies by default, or apply redaction rules.
  • Avoid capturing full stack locals unless absolutely necessary.

7. Control PII Flow in Distributed Tracing

In microservices, distributed tracing is critical—but it can also be a spreading mechanism for PII if you’re not careful.

Use non-PII correlation and trace IDs

  • Generate a random trace_id and span_id for each request.
  • Propagate only those IDs between services.
  • Don’t use emails, phone numbers, or customer names as correlation identifiers.

Then, logs and traces can be linked using these IDs without ever exposing PII.

Sanitize trace attributes

Most tracing systems let you attach attributes (tags):

  • Limit attributes to non-sensitive metadata like service, region, feature_flag.
  • Avoid attaching request payloads, query parameters, or user-supplied strings.
  • If you need user-related context, use non-sensitive IDs (user_id) instead of emails.

8. Treat Logging Systems as Sensitive Infrastructure

Many teams use weaker security controls for logging and analytics than for core production databases. That’s dangerous if logs contain customer data.

Harden your log storage

  • Restrict access: apply least privilege and RBAC; only those who truly need logs can read them.
  • Encrypt logs at rest and in transit.
  • Segment logs by environment (prod vs. staging vs. dev).
  • Apply audit logging for log access and downloads.

Use short retention by default

  • Keep logs only as long as they provide operational value (for example, 7–30 days).
  • For compliance or forensic needs, use dedicated, locked-down archival mechanisms.

Shorter retention windows reduce the blast radius of any accidental PII leak.


9. Make Privacy-Preserving Logging the Default Developer Experience

The biggest barrier to keeping PII out of logs is friction. If safe logging is painful, developers will bypass it.

Provide safe abstractions

  • Shared log utilities:
    • logInfo(event, metadata) where metadata is validated and sanitized.
    • logError(errorCode, err, metadata) that strips PII.
  • Schema-aware logging:
    • Allow only whitelisted keys in log metadata.
    • Reject or refuse to emit logs that contain forbidden keys.

Linting and CI checks

Integrate checks into your workflow:

  • Static analysis rules to flag:
    • Logging of variables named like email, phone, ssn, card, password.
    • Logging of full HTTP bodies or unfiltered request objects.
  • CI jobs that:
    • Run regex scans on logs in test environments for PII patterns.
    • Break builds when violations are detected.

Train and align the team

  • Share clear guidelines with examples of safe vs. unsafe logs.
  • Emphasize that logging user PII is both a security and compliance risk, regardless of your industry.
  • Make it explicit that privacy-preserving logging is part of coding standards, not an optional extra.

10. Continuously Monitor for PII in Logs

Even with strong patterns, mistakes happen. You need feedback loops.

Automated PII scanning

Run recurring scans on your log stores for:

  • Email-like patterns.
  • Phone numbers.
  • Card-number formats (Luhn checks).
  • Government ID formats (where applicable).

When matches are found:

  • Trigger alerts.
  • Investigate the responsible service and code path.
  • Fix the logging code, and, if necessary, purge affected logs.

Incident response playbook

Prepare a process for when PII is found in logs:

  1. Identify scope: which services, timeframes, and data types are involved.
  2. Contain: restrict access or temporarily disable log streams if needed.
  3. Eradicate: remove or anonymize affected log data.
  4. Fix: patch code and update your logging library or rules.
  5. Document: record what happened and how it was resolved to improve practices.

Final Thoughts

Preventing customer PII from ending up in application logs and error traces across microservices requires a combination of design, tooling, and culture:

  • Design APIs and URLs that don’t expose PII (especially in paths and query parameters).
  • Use tokenization and arbitrary IDs so most services never see raw PII.
  • Standardize structured, PII-aware logging libraries that make safe logging easy.
  • Harden error handling, tracing, and log storage so accidental leaks are less likely and less damaging.
  • Continuously scan for violations and treat them as real incidents to be fixed, not minor annoyances.

By treating logging with the same seriousness as your primary data stores—and by removing the complexity barrier for developers—you can drastically reduce the risk that sensitive customer data slips into logs and remains there unnoticed.

How do we stop customer PII from ending up in application logs and error traces across microservices? | Data Security Platforms | Codeables | Codeables