feat(08-01): Health monitoring & logging infrastructure
- 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
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
const { Pool } = require('pg');
|
||||
const logger = require('./logger');
|
||||
|
||||
/**
|
||||
* Health Monitoring Module
|
||||
* Tracks application health metrics including uptime and database connectivity
|
||||
*/
|
||||
|
||||
const startTime = Date.now();
|
||||
|
||||
/**
|
||||
* Get application health status
|
||||
* @returns {Object} Health status object with status, uptime, and timestamp
|
||||
*/
|
||||
async function getHealthStatus(pool) {
|
||||
try {
|
||||
// Check database connectivity
|
||||
const dbHealthStart = Date.now();
|
||||
const dbResult = await pool.query('SELECT NOW()');
|
||||
const dbHealthDuration = Date.now() - dbHealthStart;
|
||||
|
||||
const dbHealthy = dbResult.rows.length > 0;
|
||||
|
||||
return {
|
||||
status: dbHealthy ? 'healthy' : 'degraded',
|
||||
uptime: Math.floor((Date.now() - startTime) / 1000), // uptime in seconds
|
||||
timestamp: new Date().toISOString(),
|
||||
database: {
|
||||
connected: dbHealthy,
|
||||
responseTime: `${dbHealthDuration}ms`
|
||||
}
|
||||
};
|
||||
} catch (err) {
|
||||
logger.error('Health check failed', { error: err.message });
|
||||
return {
|
||||
status: 'unhealthy',
|
||||
uptime: Math.floor((Date.now() - startTime) / 1000),
|
||||
timestamp: new Date().toISOString(),
|
||||
database: {
|
||||
connected: false,
|
||||
error: err.message
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get uptime in seconds since application start
|
||||
* @returns {number} Uptime in seconds
|
||||
*/
|
||||
function getUptime() {
|
||||
return Math.floor((Date.now() - startTime) / 1000);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getHealthStatus,
|
||||
getUptime
|
||||
};
|
||||
Reference in New Issue
Block a user