From c6fd737fa8572d7bbe61ce2954f36579451816ae Mon Sep 17 00:00:00 2001 From: seohyun0120 Date: Wed, 18 Sep 2024 21:26:12 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20production=20=ED=99=98=EA=B2=BD?= =?UTF-8?q?=EC=97=90=EC=84=9C=EB=8A=94=20=EB=A1=9C=EA=B7=B8=EB=A5=BC=20?= =?UTF-8?q?=EA=B0=84=EA=B2=B0=ED=95=98=EA=B2=8C=20=EB=82=A8=EA=B8=B0?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/util/config.ts | 3 +++ src/util/logger.ts | 33 +++++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/util/config.ts b/src/util/config.ts index b87ffd4..4f56b11 100644 --- a/src/util/config.ts +++ b/src/util/config.ts @@ -19,7 +19,10 @@ const FCM_PROJECT_ID = process.env.FCM_PROJECT_ID; const FCM_PRIVATE_KEY = process.env.FCM_PRIVATE_KEY; const FCM_CLIENT_EMAIL = process.env.FCM_CLIENT_EMAIL; +const ENV = process.env.NODE_ENV; + export default { + ENV, DB_URL, PORT, AWS_ACCESS_KEY_ID, diff --git a/src/util/logger.ts b/src/util/logger.ts index 98424b4..ce73c54 100644 --- a/src/util/logger.ts +++ b/src/util/logger.ts @@ -3,6 +3,8 @@ import { randomUUID } from 'crypto'; import { Request, Response, NextFunction } from 'express'; import pino from 'pino'; +import config from './config'; + const transport = pino.transport({ targets: [ { @@ -20,24 +22,35 @@ const attachRequestId = (req: Request, res: Response, next: NextFunction) => { next(); }; +const ENV = `${config.ENV}`; +const isProduction = ENV === 'production'; +logger.info(`env: ${ENV} - ${isProduction}`); + const loggerMiddleware = (req: Request, res: Response, next: NextFunction) => { - const start = Date.now(); + const startTime = Date.now(); + const { method, originalUrl, body } = req; logger.info( - { method: req.method, url: req.originalUrl, body: req.body }, - `--> [${req.method}] ${req.originalUrl} Request called`, + isProduction ? undefined : { method, url: originalUrl, body }, + `--> ${method} ${originalUrl}`, ); const originalSend = res.send; res.send = (data) => { - logger[res.statusCode !== 200 ? 'error' : 'info']( - { - statusCode: res.statusCode, - duration: `${Date.now() - start}ms`, - response: JSON.parse(data), - }, - `<-- [${req.method}] ${res.statusCode} ${req.originalUrl} Response received in ${Date.now() - start}ms`, + const duration = Date.now() - startTime; + const statusCode = res.statusCode; + + logger[statusCode !== 200 ? 'error' : 'info']( + isProduction + ? undefined + : { + method, + url: originalUrl, + response: JSON.parse(data), + }, + `<-- ${statusCode} ${method} ${originalUrl} (${duration}ms)`, ); + return originalSend.bind(res)(data); };