keepcalculator

06 Numbers and Powers

Keep Sensitive Data Out of Application Logs

Card No. 06·69665 · Updated · Standard formula, shown below

Logging is essential for debugging and monitoring, but it can become a security liability when sensitive data—passwords, tokens, credit card numbers, personal information—ends up in log files. This guide shows you how to systematically prevent sensitive data from entering your application logs, whether you’re building a new app or securing an existing one. You’ll learn practical techniques for redaction, filtering, and safe logging practices that you can implement immediately.

What You’ll Need

  • Access to your application’s logging configuration (e.g., log4j, Logback, Winston, Python logging, or your framework’s built-in logger)
  • Ability to modify and deploy code or configuration changes
  • A list of sensitive data types relevant to your application (e.g., passwords, API keys, SSNs, credit card numbers)
  • Access to your log management system (if you use one) to verify changes

Step-by-Step

  1. Inventory sensitive data. List every type of sensitive information your application handles: authentication credentials, tokens, session IDs, personal data (names, emails, addresses), financial data (card numbers, bank accounts), health data, or any data regulated by laws like GDPR or HIPAA. Also identify where each type enters your system (e.g., request bodies, query parameters, headers, database results).
  2. Audit current logging. Search your codebase for logging statements that might include sensitive data. Look for patterns like logger.info("User " + user + " logged in") where user could be an object with sensitive fields. Also review your logging configuration for any global patterns that might log entire request/response objects.
  3. Use parameterized logging. Replace string concatenation with parameterized logging. For example, in Logback/log4j2 use logger.info("User {} logged in", userId) instead of logger.info("User " + userId + " logged in"). This prevents accidental evaluation of complex objects and makes it easier to apply redaction filters.
  4. Implement redaction filters. Configure your logging framework to automatically redact known sensitive patterns. For example, in Logback, create a custom Converter that replaces values matching regex patterns (e.g., \b\d{16}\b for credit card numbers) with [REDACTED]. In Python logging, use a Filter class to scrub sensitive fields from log records.
  5. Mask sensitive fields in objects. For objects that are logged directly (e.g., a User object), override their toString() method to exclude sensitive fields, or use a serialization wrapper that omits them. Alternatively, use a logging-specific DTO (Data Transfer Object) that only contains non-sensitive fields.
  6. Sanitize HTTP request/response logging. If you log HTTP requests and responses, ensure you strip or redact sensitive headers (e.g., Authorization, Cookie) and query parameters (e.g., ?token=...). Use middleware or interceptors to scrub these before passing to the logger.
  7. Set appropriate log levels. Avoid logging sensitive data at debug or trace levels. Review your logging level configuration in production—often INFO is sufficient, and DEBUG should only be enabled temporarily with caution.
  8. Centralize and monitor. If you use a log aggregation tool (e.g., ELK, Splunk), configure additional redaction at the ingest level as a second line of defense. Also set up alerts for any log entries that contain patterns resembling sensitive data (e.g., regex for credit card numbers).
  9. Test your changes. Write unit tests that verify sensitive data does not appear in logs. For example, create a test that triggers a log statement with a fake password and asserts that the output does not contain the password. Also perform manual testing with sample sensitive data.
  10. Review and update regularly. As your application evolves, new sensitive data types may emerge. Periodically re-audit your logging and update your redaction rules accordingly.

Common Mistakes

  • Logging exception messages that contain sensitive data. Exception messages often include user input or internal details. Always log the exception type and a sanitized message, not the full stack trace if it contains sensitive values.
  • Relying solely on log level to protect data. Setting DEBUG off doesn’t help if you log sensitive data at INFO level. Redaction must be active at all levels.
  • Forgetting about third-party libraries. Libraries you use may log request/response data themselves. Check their logging behavior and configure them to exclude sensitive data.
  • Using homegrown redaction that misses edge cases. Regex patterns can fail on variations (e.g., spaces in credit card numbers). Use well-tested libraries or multiple patterns.
  • Not testing redaction in production-like conditions. Your test environment might not have the same logging configuration as production. Ensure your redaction works in the actual deployment.

Tips

  • Use structured logging (e.g., JSON) with a schema that excludes sensitive fields. This makes it easier to apply redaction at the log management level.
  • Adopt a policy of “log by default, redact by exception”—only log specific fields you know are safe.
  • Consider using a dedicated logging library that supports built-in redaction, such as Logstash’s LogStash::Filters::Redact for Ruby, or the redact option in Winston for Node.js.
  • For cloud environments, use managed logging services that offer data masking features (e.g., AWS CloudWatch Logs data protection, GCP Cloud Logging data access logs).
  • Train your team on safe logging practices. Many leaks happen because developers are unaware of what constitutes sensitive data.

Troubleshooting

If you find sensitive data still appearing in logs after implementing these steps, consider the following:

  • Check if the data is coming from a source you missed. Use your log aggregation tool to search for the sensitive pattern and trace it back to the exact logging statement. It might be from a library or a third-party service.
  • Verify your redaction filter is actually applied. In some frameworks, filters are only applied to certain appenders or loggers. Ensure your filter is attached to the correct appender (e.g., console, file).
  • Test with the exact format of the sensitive data. For example, if you’re redacting credit card numbers, test with variations like spaces, dashes, and no separators.
  • If you’re using async logging, ensure the redaction happens before the log record is queued. Some async appenders may bypass filters.
  • Check if sensitive data is being logged by the framework itself (e.g., Spring’s RequestLoggingFilter). Disable or customize those components.

Frequently Asked Questions

What counts as sensitive data in logs?

Sensitive data includes passwords, tokens, API keys, credit card numbers, social security numbers, personal health information, and any data that could lead to identity theft or unauthorized access. Also consider data that is protected by regulations like GDPR, HIPAA, or PCI-DSS.

Can I log encrypted versions of sensitive data?

Yes, but only if the encryption is irreversible (e.g., hashing) and the hash cannot be reversed. However, it’s usually better to avoid logging the data entirely unless you have a specific debugging need.

How do I handle sensitive data in stack traces?

Sanitize exception messages before logging. If a stack trace includes variable values, consider using a custom exception handler that strips sensitive parameters. Alternatively, log only the exception class and a generic message.

Is it enough to just set log level to ERROR in production?

No. Even at ERROR level, you might log sensitive data if an error occurs during a request with sensitive parameters. Redaction is mandatory regardless of log level.

What if I’m using a third-party logging service?

Most services offer server-side redaction or data masking. Configure those features, but still implement client-side redaction as a best practice. Never rely solely on the service.

How often should I review my logging practices?

At least once per release cycle, and whenever you add new features that handle new types of user data. Also review after any security incident or compliance audit.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →