Last active
September 25, 2019 15:20
-
-
Save deepal/49136f9263d55f4e8c9d07cb94bf1289 to your computer and use it in GitHub Desktop.
Restify middleware for request logging including response code and response time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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