Use microsecond precision to log response time

This commit is contained in:
Rafa de la Torre 2018-04-20 19:04:28 +02:00
parent 145d5f91e3
commit 2d432c55d5

View File

@ -54,7 +54,7 @@ function getLogger(logger4js, options) {
if (nolog && nolog.test(req.originalUrl)) return next(); if (nolog && nolog.test(req.originalUrl)) return next();
if (thislogger.isLevelEnabled(level) || options.level === 'auto') { if (thislogger.isLevelEnabled(level) || options.level === 'auto') {
var start = new Date() var start = process.hrtime()
, statusCode , statusCode
, writeHead = res.writeHead , writeHead = res.writeHead
, url = req.originalUrl; , url = req.originalUrl;
@ -81,7 +81,8 @@ function getLogger(logger4js, options) {
//hook on end request to emit the log entry of the HTTP request. //hook on end request to emit the log entry of the HTTP request.
res.on('finish', function() { res.on('finish', function() {
res.responseTime = new Date() - start; var end = process.hrtime();
res.responseTime = (end[0] - start[0]) * 1000 + (end[1] - start[1]) / 1000000;
//status code response level handling //status code response level handling
if(res.statusCode && options.level === 'auto'){ if(res.statusCode && options.level === 'auto'){
level = levels.INFO; level = levels.INFO;