Spaces:
Sleeping
Sleeping
Download backend/src/middleware/requestLogger.ts from moustafa7/protofolio: direct link, hf CLI and curl.
- Browser
- Download file 1.17 kB
-
https://huggingface.co/spaces/moustafa7/protofolio/resolve/main/backend/src/middleware/requestLogger.ts
- Command line
-
hf download hf://spaces/moustafa7/protofolio/backend/src/middleware/requestLogger.ts
-
curl -L -o requestLogger.ts https://huggingface.co/spaces/moustafa7/protofolio/resolve/main/backend/src/middleware/requestLogger.ts
1.17 kB
| import { Request, Response, NextFunction } from "express"; | |
| import { v4 as uuidv4 } from "uuid"; | |
| import { logger } from "../config/logger"; | |
| const SKIP_PATHS = ["/api/health", "/uploads"]; | |
| export function requestLogger(req: Request, res: Response, next: NextFunction) { | |
| const requestId = | |
| (req.headers["x-request-id"] as string) || uuidv4(); | |
| req.headers["x-request-id"] = requestId; | |
| res.setHeader("X-Request-Id", requestId); | |
| if (SKIP_PATHS.some((p) => req.path.startsWith(p))) { | |
| return next(); | |
| } | |
| const start = Date.now(); | |
| res.on("finish", () => { | |
| const duration = Date.now() - start; | |
| const meta = { | |
| method: req.method, | |
| url: req.originalUrl, | |
| status: res.statusCode, | |
| duration: `${duration}ms`, | |
| requestId, | |
| ip: req.ip || req.socket.remoteAddress, | |
| }; | |
| if (res.statusCode >= 500) { | |
| logger.error(`${req.method} ${req.originalUrl} ${res.statusCode}`, meta); | |
| } else if (res.statusCode >= 400) { | |
| logger.warn(`${req.method} ${req.originalUrl} ${res.statusCode}`, meta); | |
| } else { | |
| logger.info(`${req.method} ${req.originalUrl} ${res.statusCode}`, meta); | |
| } | |
| }); | |
| next(); | |
| } | |