From 75ccdf5185a3a1d6ed47bb2324b916479f827520 Mon Sep 17 00:00:00 2001 From: Xen0Xys Date: Wed, 7 Feb 2024 17:08:46 +0100 Subject: [PATCH] :art: Fix api logger crash --- src/common/middlewares/logger.middleware.ts | 34 ++++++++++++--------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/common/middlewares/logger.middleware.ts b/src/common/middlewares/logger.middleware.ts index 54c0219..66853de 100644 --- a/src/common/middlewares/logger.middleware.ts +++ b/src/common/middlewares/logger.middleware.ts @@ -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(); }