Skip to content

Commit

Permalink
improve loggly request logging call (#15259)
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipthelen authored Jun 26, 2024
1 parent c47b287 commit 26d070f
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions website/server/middlewares/requestLogHandler.js
Original file line number Diff line number Diff line change
@@ -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);
});
Expand Down

0 comments on commit 26d070f

Please sign in to comment.