Skip to content

Instantly share code, notes, and snippets.

@deepal
Last active September 25, 2019 15:20
Show Gist options
  • Select an option

  • Save deepal/49136f9263d55f4e8c9d07cb94bf1289 to your computer and use it in GitHub Desktop.

Select an option

Save deepal/49136f9263d55f4e8c9d07cb94bf1289 to your computer and use it in GitHub Desktop.
Restify middleware for request logging including response code and response time
this.server.pre((req, res, next) => {
this.logger.debug(`Start ${req.method} ${req.url}`);
const requestStart = process.hrtime();
res.on('finish', () => {
const [reqTimeS, reqTimeNS] = process.hrtime(requestStart),
requestDurationMS = (reqTimeS * 1e3 + reqTimeNS / 1e9).toFixed(2);
this.logger.debug(`End ${req.method} ${req.url} (status=${res.statusCode} duration=${requestDurationMS}ms)`);
});
next();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment