Secure Logging Guidelines
Overview
For SOC 2 compliance, all logging must follow these guidelines to prevent PII leakage and sensitive data exposure.Rules
1. Use Structured Logger
❌ Don’t useconsole.log directly:
2. Never Log Sensitive Data
Never log:- Passwords (even hashed)
- API keys (full keys - prefixes are OK)
- Session tokens / JWTs
- HMAC secrets
- Credit card numbers
- Social Security Numbers
- Full email addresses (in production)
- Phone numbers
- Any PII without explicit need
- User IDs (CUIDs, not emails)
- Organization IDs
- Resource IDs
- Timestamps
- Action types
- Status codes
- IP addresses (for security analysis)
3. Error Handling
❌ Don’t expose internal errors to users:4. Logging Levels
Use appropriate log levels: ERROR - Something broke, needs immediate attention:5. Context is Key
Always provide context for debugging: ✅ Good:Migration Checklist
To migrate existing code:-
Find all console.log/error/warn:
-
Replace with structured logger:
console.log()→logInfo()orlogDebug()console.error()→logError()console.warn()→logWarning()
-
Audit logged data:
- Remove any PII
- Remove any secrets/tokens
- Add context objects
-
Test:
- Verify logs appear in Sentry
- Verify no sensitive data in logs
- Verify error messages are safe
Production vs Development
Development:- Logs go to console
- Detailed error messages OK
- Stack traces shown
- Logs go to Sentry
- Generic error messages only
- Stack traces not sent to client
- PII automatically redacted
Audit Log vs Application Log
Audit Logs (src/lib/audit-log.ts):
- Security events
- User actions
- System changes
- Stored in database
- Retained for 1 year minimum
- Immutable
src/lib/logger.ts):
- Application events
- Errors and warnings
- Debugging info
- Sent to Sentry
- Retained per Sentry settings
- Not regulatory requirement
Examples
API Route Logging
Background Job Logging
Testing
Test that logging works correctly:Next Steps
-
Run the migration script to find all console.log usage:
- Review each instance and replace with structured logger
- Add automated linting rule to prevent future console.log usage
- Regular audit of logs to ensure no PII leakage