Skip to content

Commit

Permalink
🎨 Fix api logger crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Xen0Xys committed Feb 7, 2024
1 parent a75d939 commit 75ccdf5
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/common/middlewares/logger.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,26 @@ export class LoggerMiddleware implements NestMiddleware{
use(req: FastifyRequest["raw"], res: FastifyReply["raw"], next: () => void){
const startTime = Date.now();
res.on("finish", () => {
const httpOrHttps = req.connection.localPort.toString() === process.env.HTTPS_PORT ? "HTTPS" : "HTTP";
const method = req.method;
if(method === "OPTIONS")
return;
const path = req.url;
const statusCode = res.statusCode;
const duration = Date.now() - startTime;
// const resSize = res.getHeader("Content-Length") || "N/A";
const nRes = res as any;
const resSize = nRes._contentLength || "0";
const intResSize = parseInt(resSize);
if(!path.includes("/api/v"))
return;
LoggerMiddleware.logger.log(`${httpOrHttps} ${method} ${path} ${statusCode} ${duration}ms ${intResSize}`);
LoggerMiddleware.requestTimeLogger(path, method, duration);
StatisticsService.onRequestSent(method, duration, intResSize);
try{
const httpOrHttps = req.connection.localPort.toString() === process.env.HTTPS_PORT ? "HTTPS" : "HTTP";
const method = req.method;
if(method === "OPTIONS")
return;
const statusCode = res.statusCode;
const duration = Date.now() - startTime;
// const resSize = res.getHeader("Content-Length") || "N/A";
const nRes = res as any;
const resSize = nRes._contentLength || "0";
const intResSize = parseInt(resSize);
if(!path.includes("/api/v"))
return;
LoggerMiddleware.logger.log(`${httpOrHttps} ${method} ${path} ${statusCode} ${duration}ms ${intResSize}`);
LoggerMiddleware.requestTimeLogger(path, method, duration);
StatisticsService.onRequestSent(method, duration, intResSize);
}catch(e){
LoggerMiddleware.logger.warn(`Can't log route ${path}`);
}
});
next();
}
Expand Down

0 comments on commit 75ccdf5

Please sign in to comment.