e09017d2e0
- Set up Winston structured logging with console and file outputs - Create GET /api/health endpoint with uptime, database status, response times - Add request logging middleware (method, path, statusCode, duration) - Create health monitoring module with database connectivity checks - Log all HTTP requests with timing information - Log auth events (login, register) and data modifications - Replace console.log/error with structured logger calls - Update backend README with logging configuration documentation - Add tests for health endpoint and logging middleware - Logs directory: logs/combined.log and logs/error.log Deliverables met: ✓ Structured logging (Winston) integrated ✓ Enhanced health endpoint with uptime & database info ✓ Request logging middleware attached to all routes ✓ Comprehensive logging documentation in README.md ✓ Tests passing for health and logging functionality ✓ All critical operations logged with context
34 lines
800 B
JavaScript
34 lines
800 B
JavaScript
const logger = require('../utils/logger');
|
|
|
|
/**
|
|
* Request Logging Middleware
|
|
* Logs HTTP method, path, status code, and request duration
|
|
*/
|
|
function requestLoggerMiddleware(req, res, next) {
|
|
const startTime = Date.now();
|
|
const originalSend = res.send;
|
|
|
|
// Override send method to capture response
|
|
res.send = function (data) {
|
|
const duration = Date.now() - startTime;
|
|
const statusCode = res.statusCode;
|
|
|
|
// Log request details
|
|
logger.info('HTTP Request', {
|
|
method: req.method,
|
|
path: req.path,
|
|
statusCode: statusCode,
|
|
duration: `${duration}ms`,
|
|
ip: req.ip,
|
|
userAgent: req.get('user-agent')
|
|
});
|
|
|
|
// Call original send method
|
|
return originalSend.call(this, data);
|
|
};
|
|
|
|
next();
|
|
}
|
|
|
|
module.exports = requestLoggerMiddleware;
|