From 8d5b29ff421fc8bca2d80fd7ab24543388ba94ef Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 5 Jun 2025 22:08:57 +0000 Subject: [PATCH] feat: Implement custom request logging for API Replaced external 'morgan' library with a native custom logging middleware in api/server.js. The new middleware logs: - Incoming requests: Timestamp, HTTP method, URL, and client IP. - Outgoing responses: Timestamp, status code, status message, original method, URL, and request duration. This enhances debugging capabilities by providing clear insights into API traffic and performance directly from the server logs. --- api/server.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/api/server.js b/api/server.js index cbef05c..1f16570 100644 --- a/api/server.js +++ b/api/server.js @@ -9,9 +9,13 @@ import asistenciasRouter from './routes/asistencias/asistencias.js'; import tareasRouter from './routes/tareas/tareas.js'; import planillasRouter from './routes/planillas/planillas.js'; - - - +// Helper function for logging duration +const getDurationInMilliseconds = (start) => { + const NS_PER_SEC = 1e9; + const NS_TO_MS = 1e-6; + const diff = process.hrtime(start); + return (diff[0] * NS_PER_SEC + diff[1]) * NS_TO_MS; +}; // Resto del código @@ -22,6 +26,18 @@ const prisma = new PrismaClient(); export const app = express(); app.use(express.json()); +// Custom Logging Middleware +app.use((req, res, next) => { + const start = process.hrtime(); + console.log(`[${new Date().toISOString()}] Incoming Request: ${req.method} ${req.originalUrl} from ${req.ip}`); + + res.on('finish', () => { + const durationInMilliseconds = getDurationInMilliseconds(start); + console.log(`[${new Date().toISOString()}] Response Sent: ${res.statusCode} ${res.statusMessage}; ${req.method} ${req.originalUrl}; Duration: ${durationInMilliseconds.toLocaleString()} ms`); + }); + + next(); +}); app.use(cors({ origin: ['http://localhost:5173', 'https://planilla.interno.com'],