audit_logs table using db.query(), and Winston for structured application logs.
What Is Accountability
Accountability ensures the system can answer: who did what, when, from where, and with what result?Who
User ID and role from the JWT (req.user.id)
What
Action: CREATE, UPDATE, DELETE, LOGIN
When
Exact timestamp recorded in the DB
Where
Client IP address and browser user-agent
On What
Resource type (users, posts) and specific ID
Result
SUCCESS or FAILURE based on HTTP status code
MySQL audit_logs Table
Audit Log Service
Using Audit Logs in Routes
CallcreateAuditLog() after the DB operation inside the callback:
Morgan: HTTP Request Logging
dev format):
Winston: Application Logging
Log Levels
What NOT to Log
Querying Audit Logs
Key Terms
Common Mistakes
Logging sensitive data
Logging sensitive data
Always call sanitizeBody() before passing request body to createAuditLog(). Logging passwords is a GDPR violation.
Crashing the request on audit log failure
Crashing the request on audit log failure
Audit log failures should be caught and printed to console only. Never let a failed INSERT into audit_logs break the API response.
No indexes on audit_logs
No indexes on audit_logs
Without indexes on user_id, action, and created_at, queries over a large audit_logs table take seconds. Always define indexes.