diff --git a/website/server/middlewares/requestLogHandler.js b/website/server/middlewares/requestLogHandler.js index b7d3a499a7d..7191e116c0f 100644 --- a/website/server/middlewares/requestLogHandler.js +++ b/website/server/middlewares/requestLogHandler.js @@ -1,28 +1,35 @@ import { v4 as uuid } from 'uuid'; +import omit from 'lodash/omit'; import logger from '../libs/logger'; -export const logRequestEnd = (req, res) => { - const now = Date.now(); - const requestTime = now - req.requestStartTime; - logger.info('Request completed', { +function buildBaseLogData (req) { + return { requestId: req.requestIdentifier, method: req.method, url: req.originalUrl, - duration: requestTime, - endTime: now, - statusCode: res.statusCode, - }); + + headers: omit(req.headers, ['x-api-key', 'cookie', 'password', 'confirmPassword']), + body: omit(req.body, ['password', 'confirmPassword']), + query: omit(req.query, ['password', 'confirmPassword']), + }; +} + +export const logRequestEnd = (req, res) => { + const now = Date.now(); + const requestTime = now - req.requestStartTime; + const data = buildBaseLogData(req); + data.duration = requestTime; + data.endTime = now; + data.statusCode = res.statusCode; + logger.info('Request completed', data); }; export const logRequestData = (req, res, next) => { req.requestStartTime = Date.now(); req.requestIdentifier = uuid(); - logger.info('Request started', { - requestId: req.requestIdentifier, - method: req.method, - url: req.originalUrl, - startTime: req.requestStartTime, - }); + const data = buildBaseLogData(req); + data.startTime = req.requestStartTime; + logger.info('Request started', data); req.on('close', () => { logRequestEnd(req, res); });