📡 Event-Driven Architecture
To avoid coupling the audit logic within controllers, the application leverages Laravel’s Events and Listeners system. For example, during the authentication flow, theAuthController only issues the token. It is the AuditLoginListener that passively intercepts the event, collects the User-Agent, captures the request context, and inserts it into the audit log’s JSON payload.
🕵️♂️ Privacy by Design
To comply with modern data protection regulations for users (where an IP address is considered personal information), the system does not store exact addresses. This responsibility was extracted into a reusable Trait (AuditableIp).
🔄 Automatic Rotation and Maintenance (Model Events)
To ensure that the database does not become saturated with unnecessary historical records and to optimize query performance, the system features a “log rotation” mechanism integrated directly into the Eloquent model lifecycle. Through thebooted() method, the AuditLog model intercepts the creation event. Every time a new record is inserted, the system dynamically checks if the maximum allowed threshold (50 events) has been exceeded and, if so, automatically purges the oldest records.