There is a small trick to configure an expressjs middleware function to capture a request's run time and status. Here it is:
const serverLogger = (req, res, next) => {
const startMs = Date.now();
res.on("finish", () => {
log.info(
{
req: {
path: req.path,
},
res: {
statusCode: res.statusCode,
},
timeMs: (Date.now() - startMs),
},
);
});
next();
};
app.use(serverLogger);
No comments:
Post a Comment