Repository: fastify/fastify (JavaScript)

Issue:
Logger responseTime is null on 1.0.0-rc.3
on *version 1.0.0-rc.3* the logger responseTime return null.

this line return `undefined` instead of the current time
https://github.com/fastify/fastify/commit/f545accb2976d54a63a573cc187a0b0472cba972#diff-20da0b6140a4768d182af0d9b185ac28R208

```js
const fastify = Fastify({
  logger: true
});
```




Discussion:
cc @allevo
Good spot!



Patch:
diff --git a/fastify.js b/fastify.js
index 5cd26105ad0..370c79f32d8 100644
--- a/fastify.js
+++ b/fastify.js
@@ -43,14 +43,14 @@ function build (options) {
   }
 
   var log
-  var hasLogger = false
+  var hasLogger = true
   if (isValidLogger(options.logger)) {
     log = loggerUtils.createLogger({
       logger: options.logger,
       serializers: Object.assign({}, loggerUtils.serializers, options.logger.serializers)
     })
   } else if (!options.logger) {
-    hasLogger = true
+    hasLogger = false
     log = Object.create(abstractLogging)
     log.child = () => log
   } else {
@@ -76,7 +76,7 @@ function build (options) {
   const genReqId = customGenReqId || loggerUtils.reqIdGenFactory()
   const now = loggerUtils.now
   const onResponseIterator = loggerUtils.onResponseIterator
-  const onResponseCallback = hasLogger ? noop : loggerUtils.onResponseCallback
+  const onResponseCallback = hasLogger ? loggerUtils.onResponseCallback : noop
 
   const app = avvio(fastify, {
     autostart: false
@@ -202,13 +202,12 @@ function build (options) {
     req.id = genReqId(req)
     req.log = res.log = log.child({ reqId: req.id, level: context.logLevel })
     req.originalUrl = req.url
+    res._startTime = hasLogger ? now() : undefined
+    res._context = context
 
     req.log.info({ req }, 'incoming request')
 
-    res._startTime = hasLogger ? now() : undefined
-
-    if (hasLogger === false || context.onResponse !== null) {
-      res._context = context
+    if (hasLogger === true || context.onResponse !== null) {
       res.on('finish', onResFinished)
       res.on('error', onResFinished)
     }
diff --git a/lib/logger.js b/lib/logger.js
index 724b1c8d07f..eeb23f6a547 100644
--- a/lib/logger.js
+++ b/lib/logger.js
@@ -65,7 +65,11 @@ function now () {
 }
 
 function onResponseCallback (err, res) {
-  var responseTime = now() - res._startTime
+  var responseTime = 0
+
+  if (res._startTime) {
+    responseTime = now() - res._startTime
+  }
 
   if (err) {
     res.log.error({
diff --git a/package.json b/package.json
index 3e1949aadf3..8f9d81a60e9 100644
--- a/package.json
+++ b/package.json
@@ -83,6 +83,7 @@
     "ienoopen": "^1.0.0",
     "joi": "~11.3.4",
     "pre-commit": "^1.2.2",
+    "proxyquire": "^2.0.0",
     "pump": "^3.0.0",
     "semver": "^5.5.0",
     "send": "^0.16.1",


Test patch:
diff --git a/test/internals/logger.test.js b/test/internals/logger.test.js
index 235683b1070..8de57057135 100644
--- a/test/internals/logger.test.js
+++ b/test/internals/logger.test.js
@@ -3,6 +3,8 @@
 const t = require('tap')
 const test = t.test
 const Fastify = require('../..')
+const split = require('split2')
+const proxyquire = require('proxyquire')
 const loggerUtils = require('../../lib/logger')
 
 test('time resolution', t => {
@@ -72,6 +74,39 @@ test('The logger should reuse request id header for req.id', t => {
   })
 })
 
+test('The logger should add a timestamp if logging to stdout', t => {
+  t.plan(5)
+
+  const stream = split(JSON.parse)
+  const stub = Object.create(loggerUtils)
+  stub.createLogger = function (opts) {
+    return loggerUtils.createLogger(opts, stream)
+  }
+
+  const Fastify = proxyquire('../..', {
+    './lib/logger': stub
+  })
+  const fastify = Fastify({ logger: true })
+
+  stream.once('data', (line) => {
+    t.equal(line.msg, 'incoming request')
+    stream.once('data', (line) => {
+      t.equal(line.msg, 'Not found')
+      stream.once('data', (line) => {
+        t.equal(line.msg, 'request completed')
+        t.ok(line.responseTime, 'responseTime exists')
+      })
+    })
+  })
+
+  fastify.inject({
+    method: 'GET',
+    url: '/'
+  }, function (err, res) {
+    t.error(err)
+  })
+})
+
 function Queue () {
   this.q = []
   this.running = false
diff --git a/test/logger.test.js b/test/logger.test.js
index 8910b157ea6..b456c81b4cc 100644
--- a/test/logger.test.js
+++ b/test/logger.test.js
@@ -10,7 +10,7 @@ const pino = require('pino')
 const os = require('os')
 
 test('defaults to info level', t => {
-  t.plan(12)
+  t.plan(13)
   var fastify = null
   var stream = split(JSON.parse)
   try {
@@ -28,31 +28,33 @@ test('defaults to info level', t => {
     reply.send({ hello: 'world' })
   })
 
-  fastify.listen(0, err => {
-    t.error(err)
-    fastify.server.unref()
+  stream.once('data', listenAtLogLine => {
+    t.ok(listenAtLogLine, 'listen at log message is ok')
 
-    http.get('http://localhost:' + fastify.server.address().port)
-    stream.once('data', listenAtLogLine => {
-      t.ok(listenAtLogLine, 'listen at log message is ok')
+    stream.once('data', line => {
+      const id = line.reqId
+      t.ok(line.reqId, 'reqId is defined')
+      t.ok(line.req, 'req is defined')
+      t.equal(line.msg, 'incoming request', 'message is set')
+      t.equal(line.req.method, 'GET', 'method is get')
 
       stream.once('data', line => {
-        const id = line.reqId
+        t.equal(line.reqId, id)
         t.ok(line.reqId, 'reqId is defined')
-        t.ok(line.req, 'req is defined')
-        t.equal(line.msg, 'incoming request', 'message is set')
-        t.equal(line.req.method, 'GET', 'method is get')
-
-        stream.once('data', line => {
-          t.equal(line.reqId, id)
-          t.ok(line.reqId, 'reqId is defined')
-          t.ok(line.res, 'res is defined')
-          t.equal(line.msg, 'request completed', 'message is set')
-          t.equal(line.res.statusCode, 200, 'statusCode is 200')
-        })
+        t.ok(line.res, 'res is defined')
+        t.equal(line.msg, 'request completed', 'message is set')
+        t.equal(line.res.statusCode, 200, 'statusCode is 200')
+        t.ok(line.responseTime, 'responseTime is defined')
       })
     })
   })
+
+  fastify.listen(0, err => {
+    t.error(err)
+    fastify.server.unref()
+
+    http.get('http://localhost:' + fastify.server.address().port)
+  })
 })
 
 test('test log stream', t => {