This guide explains how to use the enhanced audit logging system in the College ERP backend.
The audit logging system tracks all critical actions performed by users in the system. It captures detailed information including:
- User who performed the action
- Action performed
- Entity affected
- Entity ID
- Payload/data related to the action
- IP address of the client
- User agent of the client
- Timestamp of the action
The AuditLog model in Prisma schema includes:
userId: ID of the user who performed the actionaction: Description of the action (e.g., CREATE_STUDENT, UPDATE_DEPARTMENT)entity: Type of entity affected (e.g., Student, Department)entityId: ID of the specific entity affectedpayload: JSON data containing details about the actionipAddress: IP address of the clientuserAgent: User agent string of the clienttimestamp: When the action occurred
The auditLogger.js utility provides functions for centralized audit logging:
Manually log an audit entry.
const { logAudit } = require('../utils/auditLogger');
await logAudit({
userId: req.user.id,
action: 'CUSTOM_ACTION',
entity: 'CustomEntity',
entityId: entityId,
payload: { key: 'value' },
req // Pass the request object to capture IP and user agent
});Express middleware for automatic audit logging.
const { auditMiddleware } = require('../utils/auditLogger');
router.post('/students',
auditMiddleware('CREATE_STUDENT', 'Student'),
studentController.createStudent
);Express middleware to log successful responses.
const { auditSuccessMiddleware } = require('../utils/auditLogger');
router.use(auditSuccessMiddleware());The audit controller provides endpoints for viewing and exporting audit logs:
GET /audit: Get all audit logs with filteringGET /audit/:id: Get specific audit logGET /audit/entity/:entity/:entityId: Get audit logs for specific entityGET /audit/user/:userId: Get user activity logsGET /audit/export: Export audit logs (JSON/CSV)GET /audit/stats: Get audit statisticsGET /audit/recent: Get recent audit logs
const { logAudit } = require('../utils/auditLogger');
exports.createStudent = async (req, res, next) => {
try {
// ... validation and creation logic ...
const student = await prisma.student.create({
// ... student data ...
});
// Log audit entry
await logAudit({
userId: req.user.id,
action: 'CREATE_STUDENT',
entity: 'Student',
entityId: student.id,
payload: { name, email, phone, courseId, sessionId },
req
});
res.status(201).json({
status: 'success',
data: { student }
});
} catch (error) {
next(error);
}
};const express = require('express');
const router = express.Router();
const { auditMiddleware, auditSuccessMiddleware } = require('../utils/auditLogger');
// Apply middleware to routes
router.post('/departments',
auditMiddleware('CREATE_DEPARTMENT', 'Department'),
departmentController.createDepartment
);
// Apply success middleware to automatically log responses
router.use(auditSuccessMiddleware());
module.exports = router;GET /audit
Query Parameters:
- userId: Filter by user ID
- action: Filter by action (partial match)
- entity: Filter by entity type (partial match)
- entityId: Filter by entity ID
- startDate: Filter by start date (ISO format)
- endDate: Filter by end date (ISO format)
- page: Page number (default: 1)
- limit: Results per page (default: 50)
GET /audit/:id
GET /audit/entity/:entity/:entityId
GET /audit/user/:userId
GET /audit/export
Query Parameters:
- startDate: Filter by start date (ISO format)
- endDate: Filter by end date (ISO format)
- format: Export format (json or csv, default: json)
GET /audit/stats
Query Parameters:
- days: Number of days to include in statistics (default: 30)
GET /audit/recent
Query Parameters:
- limit: Number of recent logs to retrieve (default: 50)
- Always log critical actions: Log all CRUD operations and important state changes
- Include relevant payload data: Include enough information to understand what happened
- Use consistent action naming: Follow the pattern VERB_ENTITY (e.g., CREATE_STUDENT, UPDATE_DEPARTMENT)
- Pass the request object: Always pass the req object to capture IP and user agent
- Handle errors gracefully: Audit logging failures should not break the main functionality
- Respect privacy: Do not log sensitive information like passwords or personal identification numbers
- Only ADMIN users can access audit logs
- HOD users can only access audit logs for their own entities
- All audit log access is itself logged for accountability
- IP addresses and user agents are stored for security analysis
- Regular export and backup of audit logs is recommended for compliance