Course contentsShow
← System Design
Lesson 1163 of 1,91929. Observability: LoggingFree lesson

Avoid Logging Sensitive Data

Never log passwords, tokens, PII, or credit cards; implement scrubbing for accidental leaks.

Avoid Logging Sensitive Data

What You'll Learn

You'll learn exactly what sensitive data must never appear in logs and how to implement safeguards to prevent accidental leaks.


The Core Problem

Logs are powerful for debugging and monitoring, but they're also widely accessible and often stored long-term. If you log passwords, authentication tokens, credit card numbers, or personally identifiable information (PII) like Social Security numbers or addresses, you've created a security time bomb. Anyone with log access—engineers, support staff, attackers who breach your logging system—can steal this data.

What Never to Log

  • Credentials: Passwords, API keys, secret tokens, OAuth tokens
  • Financial data: Credit card numbers, CVVs, bank account details
  • PII: SSNs, passport numbers, full addresses, phone numbers, email addresses (context-dependent)
  • Health information: Medical records, diagnoses (HIPAA-protected)
  • Session identifiers: Full session tokens (use truncated versions if needed)

Scrubbing and Redaction

Even with best intentions, sensitive data can slip into logs through:

  • Exception stack traces that capture method parameters
  • Request/response bodies logged for debugging
  • Database query strings with embedded values

Implement scrubbing: Use logging middleware or filters that detect and redact sensitive patterns before writing logs. Replace password=secret123 with password=***REDACTED*** or creditCard=1234-****-****-5678.

Think of it like a security checkpoint: every log message passes through a filter that masks sensitive fields automatically.

Practical Safeguards

  • Code reviews: Check logging statements for exposed secrets
  • Static analysis: Tools that flag potential credential logging
  • Log audits: Periodically scan stored logs for sensitive patterns
  • Training: Teach developers what constitutes sensitive data

Key Takeaway: Never log passwords, tokens, PII, credit cards, or other sensitive data; implement automated scrubbing to catch accidental leaks and treat logs as potentially public documents.