The authentication API implements a professional, tiered activity logging system that balances security audit requirements with database performance and storage costs.
Events are categorized by security importance:
- Critical: Authentication changes, security events (1-year retention)
- Important: Verification events, profile changes (6-month retention)
- Informational: Routine operations (3-month retention)
- High-frequency events (TOKEN_REFRESH, PROFILE_ACCESS) are disabled by default
- Anomaly detection logs unusual patterns even for disabled events
- Configurable sampling for enabled informational events
- Background service runs daily to delete expired logs
- Retention policies based on event severity
- Graceful batch processing to avoid database locks
Logs informational events when unusual patterns detected:
- First access from a new IP address
- New device or browser (user agent change)
- Access from unusual times (optional)
Out of the box, the system:
- ✅ Logs all critical and important events
- ❌ Does NOT log TOKEN_REFRESH (happens every 15 minutes)
- ❌ Does NOT log PROFILE_ACCESS (happens on every profile view)
- ✅ DOES log TOKEN_REFRESH/PROFILE_ACCESS if anomaly detected
- ✅ Automatically cleans up logs older than retention period
- ✅ Runs cleanup daily at midnight
# Completely disable specific events (comma-separated)
LOG_DISABLED_EVENTS=TOKEN_REFRESH,PROFILE_ACCESS
# Or enable specific informational events
LOG_TOKEN_REFRESH=true # Enable token refresh logging
LOG_PROFILE_ACCESS=true # Enable profile access loggingWhen informational events are enabled, control how many get logged:
# Log only 1% of token refreshes (reduces 99% of log entries)
LOG_SAMPLE_TOKEN_REFRESH=0.01
# Log only 5% of profile accesses
LOG_SAMPLE_PROFILE_ACCESS=0.05
# Log 100% (all) - same as no sampling
LOG_SAMPLE_TOKEN_REFRESH=1.0# Enable/disable anomaly detection entirely
LOG_ANOMALY_DETECTION_ENABLED=true
# Control what triggers anomaly logging
LOG_ANOMALY_NEW_IP=true # Log when user accesses from new IP
LOG_ANOMALY_NEW_USER_AGENT=true # Log when user uses new device/browser
LOG_ANOMALY_UNUSUAL_TIME=false # Log unusual time access (requires more data)
# How long to remember user patterns (default: 30 days)
LOG_ANOMALY_SESSION_WINDOW=720h # 720 hours = 30 days# Customize how long logs are kept (in days)
LOG_RETENTION_CRITICAL=365 # Critical events: 1 year
LOG_RETENTION_IMPORTANT=180 # Important events: 6 months
LOG_RETENTION_INFORMATIONAL=90 # Informational events: 3 months
# Example: Keep critical events for 2 years
LOG_RETENTION_CRITICAL=730# Enable/disable automatic cleanup
LOG_CLEANUP_ENABLED=true
# How often to run cleanup (Go duration format)
LOG_CLEANUP_INTERVAL=24h # Every 24 hours
LOG_CLEANUP_INTERVAL=12h # Every 12 hours
LOG_CLEANUP_INTERVAL=168h # Every week
# Batch size for cleanup operations
LOG_CLEANUP_BATCH_SIZE=1000 # Delete 1000 logs per batch
LOG_CLEANUP_BATCH_SIZE=5000 # Larger batches (faster but more DB load)Log everything, keep it longer:
# Log all events including informational
LOG_TOKEN_REFRESH=true
LOG_PROFILE_ACCESS=true
# But sample them to reduce volume
LOG_SAMPLE_TOKEN_REFRESH=0.1 # Log 10%
LOG_SAMPLE_PROFILE_ACCESS=0.1
# Keep logs longer
LOG_RETENTION_CRITICAL=730 # 2 years
LOG_RETENTION_IMPORTANT=365 # 1 year
LOG_RETENTION_INFORMATIONAL=180 # 6 months
# Aggressive anomaly detection
LOG_ANOMALY_UNUSUAL_TIME=trueMinimize logging, aggressive cleanup:
# Only log critical events
LOG_DISABLED_EVENTS=PROFILE_ACCESS,TOKEN_REFRESH,EMAIL_VERIFY,PROFILE_UPDATE
# Shorter retention
LOG_RETENTION_CRITICAL=180 # 6 months
LOG_RETENTION_IMPORTANT=90 # 3 months
LOG_RETENTION_INFORMATIONAL=30 # 1 month
# More frequent cleanup
LOG_CLEANUP_INTERVAL=6h # Every 6 hours
LOG_CLEANUP_BATCH_SIZE=5000Log everything for debugging:
# Enable all events
LOG_TOKEN_REFRESH=true
LOG_PROFILE_ACCESS=true
# No sampling (log 100%)
LOG_SAMPLE_TOKEN_REFRESH=1.0
LOG_SAMPLE_PROFILE_ACCESS=1.0
# Disable cleanup (manual cleanup)
LOG_CLEANUP_ENABLED=false
# Short retention for automatic cleanup if enabled
LOG_RETENTION_CRITICAL=7
LOG_RETENTION_IMPORTANT=7
LOG_RETENTION_INFORMATIONAL=7Focus on minimal data collection:
# Disable informational events
LOG_DISABLED_EVENTS=TOKEN_REFRESH,PROFILE_ACCESS
# Disable anomaly detection (less tracking)
LOG_ANOMALY_DETECTION_ENABLED=false
# Aggressive cleanup
LOG_RETENTION_CRITICAL=90
LOG_RETENTION_IMPORTANT=60
LOG_RETENTION_INFORMATIONAL=30The activity_logs table includes these fields:
CREATE TABLE activity_logs (
id UUID PRIMARY KEY,
user_id UUID NOT NULL,
event_type VARCHAR(50) NOT NULL,
timestamp TIMESTAMP WITH TIME ZONE NOT NULL,
ip_address VARCHAR(45),
user_agent TEXT,
details JSONB,
-- Smart logging fields
severity VARCHAR(20) NOT NULL DEFAULT 'INFORMATIONAL',
expires_at TIMESTAMP WITH TIME ZONE,
is_anomaly BOOLEAN NOT NULL DEFAULT false
);Use the database to check cleanup effectiveness:
-- Count logs by severity
SELECT severity, COUNT(*) as count
FROM activity_logs
GROUP BY severity;
-- Check logs near expiration
SELECT severity, COUNT(*) as expiring_soon
FROM activity_logs
WHERE expires_at < NOW() + INTERVAL '7 days'
GROUP BY severity;
-- Count expired logs waiting for cleanup
SELECT COUNT(*) as expired_count
FROM activity_logs
WHERE expires_at < NOW();If needed, trigger manual cleanup:
-- Delete expired logs manually
DELETE FROM activity_logs
WHERE expires_at < NOW();
-- Delete logs for specific user (GDPR right to be forgotten)
DELETE FROM activity_logs
WHERE user_id = 'user-uuid-here';Monitor these metrics:
- Database size growth rate
- Cleanup execution time
- Query performance on activity_logs table
- Anomaly detection rate
See migrations/README_SMART_LOGGING.md for detailed migration instructions.
-
Apply the database migration:
psql -U your_user -d your_database -f migrations/20240103_add_activity_log_smart_fields.sql
-
Restart the application (it will use the new logging system automatically)
-
Configure via environment variables if needed
-
Monitor database size and adjust settings as needed
Problem: Database growing too fast Solutions:
- Disable more events:
LOG_DISABLED_EVENTS=TOKEN_REFRESH,PROFILE_ACCESS,PROFILE_UPDATE - Increase sampling rates:
LOG_SAMPLE_TOKEN_REFRESH=0.001(0.1%) - Reduce retention:
LOG_RETENTION_INFORMATIONAL=30
Problem: Missing important security events Solutions:
- Enable anomaly detection:
LOG_ANOMALY_DETECTION_ENABLED=true - Enable specific events:
LOG_TOKEN_REFRESH=truewith sampling - Increase retention:
LOG_RETENTION_CRITICAL=730(2 years)
Problem: Old logs not being deleted Solutions:
- Check cleanup is enabled:
LOG_CLEANUP_ENABLED=true - Verify application is running continuously
- Check database logs for errors
- Manually run cleanup query to test permissions
Problem: Too many anomalies being detected Solutions:
- Increase session window:
LOG_ANOMALY_SESSION_WINDOW=1440h(60 days) - Disable specific anomaly checks:
LOG_ANOMALY_UNUSUAL_TIME=false - Review user behavior patterns (mobile users, VPNs, etc.)
- Start Conservative: Begin with default settings and monitor
- Tune Gradually: Adjust based on actual traffic patterns
- Monitor First: Watch database size for a week before changing retention
- Security First: Don't disable critical events to save storage
- Test Cleanup: Verify cleanup works before relying on it
- Document Changes: Keep track of configuration changes
- Review Regularly: Audit logging configuration quarterly
For issues or questions:
- Review this guide and
docs/API.md - Check
migrations/README_SMART_LOGGING.mdfor migration help - Review code:
internal/log/andinternal/config/logging.go - Examine the plan:
professional.plan.md