# Security Logging and Audit Apply these rules when adding or changing logging, audit trails, monitoring, or alerting. * Log security events with who, what, when, where, and outcome: authentication successes and failures, authorization failures, session and token failures, input validation failures at trust boundaries, privilege and role changes, administrative actions, sensitive data access, key rotation, data import or export, configuration changes, and certificate or TLS failures. * Prevent log injection: use structured logging with constant message templates and named parameters, never string concatenation; neutralize CR, LF, and delimiters in untrusted values, encode for the log format, and cap the length of user-supplied values. * Never log passwords, authentication secrets, session IDs, access or refresh tokens, encryption keys, connection strings, payment data, or unnecessary personal data. Hash a token when you need to correlate it. * Keep audit records append-only and tamper evident, restrict and review read access to logs, ship them over secure transport, and alert when logging stops. * Correlate requests across services with an ID that carries no secrets or personal data. * Set log retention to what operations, security, and the law require, and delete older records.