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.
This commit is contained in:
@@ -9,9 +9,13 @@ import asistenciasRouter from './routes/asistencias/asistencias.js';
|
|||||||
import tareasRouter from './routes/tareas/tareas.js';
|
import tareasRouter from './routes/tareas/tareas.js';
|
||||||
import planillasRouter from './routes/planillas/planillas.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
|
// Resto del código
|
||||||
|
|
||||||
@@ -22,6 +26,18 @@ const prisma = new PrismaClient();
|
|||||||
export const app = express();
|
export const app = express();
|
||||||
app.use(express.json());
|
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({
|
app.use(cors({
|
||||||
origin: ['http://localhost:5173', 'https://planilla.interno.com'],
|
origin: ['http://localhost:5173', 'https://planilla.interno.com'],
|
||||||
|
|||||||
Reference in New Issue
Block a user