Audit Log Cleanup
MXCP provides automated audit log cleanup to manage storage and comply with data retention policies.
Overview
Audit schemas can define retention policies that specify how long audit records should be kept:
# In your audit schema definition
retention_days: 90 # Keep records for 90 days
The mxcp log-cleanup
command applies these retention policies by deleting records older than the specified retention period.
Manual Cleanup
Run cleanup manually:
# Apply retention policies
mxcp log-cleanup
# Preview what would be deleted (dry run)
mxcp log-cleanup --dry-run
# Use specific profile
mxcp log-cleanup --profile production
# Output as JSON
mxcp log-cleanup --json
Automated Cleanup
Using Cron
Add to your crontab to run daily at 2 AM:
# Edit crontab
crontab -e
# Add this line
0 2 * * * cd /path/to/your/mxcp/project && /usr/bin/mxcp log-cleanup
Using systemd
-
Copy the provided systemd files:
sudo cp examples/systemd/mxcp-audit-cleanup.* /etc/systemd/system/
-
Edit the service file to match your environment:
sudo nano /etc/systemd/system/mxcp-audit-cleanup.service
-
Enable and start the timer:
sudo systemctl daemon-reload
sudo systemctl enable mxcp-audit-cleanup.timer
sudo systemctl start mxcp-audit-cleanup.timer -
Check status:
systemctl status mxcp-audit-cleanup.timer
systemctl list-timers mxcp-audit-cleanup.timer
Retention Policy Guidelines
When setting retention policies, consider:
- Regulatory requirements: Some data must be kept for specific periods
- Storage costs: Longer retention requires more disk space
- Performance: Large audit logs can slow down queries
- Business needs: How long do you need historical data?
Common retention periods:
- Authentication logs: 365 days (regulatory)
- API access logs: 90 days (operational)
- Debug logs: 30 days (troubleshooting)
Monitoring Cleanup
View cleanup results:
# Check systemd logs
journalctl -u mxcp-audit-cleanup.service
# Run with debug output
mxcp log-cleanup --debug
# Get JSON output for monitoring
mxcp log-cleanup --json | jq
Example JSON output:
{
"status": "ok",
"result": {
"status": "success",
"message": "Deleted 1523 records",
"deleted_per_schema": {
"api_operations:1": 1200,
"debug_logs:1": 323
}
}
}
Best Practices
- Test with dry-run first: Always use
--dry-run
before scheduling automated cleanup - Monitor disk space: Set up alerts for audit log directory size
- Backup before cleanup: Consider backing up old audit logs before deletion
- Gradual rollout: Start with shorter retention periods and increase as needed
- Multiple profiles: Run separate cleanup jobs for different profiles if needed