Merge pull request #39 from josedario87/improve-api-logging
feat: Implement custom request logging for API
This commit is contained in:
@@ -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'],
|
||||
|
||||
Reference in New Issue
Block a user