Repository: pinojs/pino
Pull request #333 (2017-11-17 02:40:19)

Fix bug added to pretty after #324 merge
It seems that after #324 was merged a bug was introduced to pretty.
It would consider lines missing hostname and pid invalid, which is not true after #324 

I'm not familiar with the code at all so not sure it detecting a pino line only with "v" is ok.
Let me know if there is anything that needs to change and I'll happily adapt =D


Patch:
diff --git a/pretty.js b/pretty.js
index d52bbb1..018a799 100755
--- a/pretty.js
+++ b/pretty.js
@@ -46,10 +46,7 @@ function filter (value, messageKey, eol) {
 }
 
 function isPinoLine (line) {
-  return line &&
-    line.hasOwnProperty('hostname') &&
-    line.hasOwnProperty('pid') &&
-    (line.hasOwnProperty('v') && line.v === 1)
+  return line && (line.hasOwnProperty('v') && line.v === 1)
 }
 
 function pretty (opts) {
@@ -121,21 +118,40 @@ function pretty (opts) {
       }) + eol
     }
 
-    line += ' ('
-    if (value.name) {
-      line += value.name + '/'
+    if (value.name || value.pid || value.hostname) {
+      line += ' ('
+
+      if (value.name) {
+        line += value.name
+      }
+
+      if (value.name && value.pid) {
+        line += '/' + value.pid
+      } else if (value.pid) {
+        line += value.pid
+      }
+
+      if (value.hostname) {
+        line += ' on ' + value.hostname
+      }
+
+      line += ')'
     }
-    line += value.pid + ' on ' + value.hostname + ')'
+
     line += ': '
+
     if (value[messageKey]) {
       line += ctx.cyan(value[messageKey])
     }
+
     line += eol
+
     if (value.type === 'Error') {
       line += '    ' + withSpaces(value.stack, eol) + eol
     } else {
       line += filter(value, messageKey, eol)
     }
+
     return line
   }
 


Test patch:
diff --git a/test/pretty.test.js b/test/pretty.test.js
index ae4ae1d..0b7efe9 100644
--- a/test/pretty.test.js
+++ b/test/pretty.test.js
@@ -133,6 +133,95 @@ test('handles missing time', function (t) {
   t.deepEqual(lines, ['{"hello":"world"}'], 'preserved lines')
 })
 
+test('handles missing pid, hostname and name', function (t) {
+  t.plan(1)
+
+  var prettier = pretty()
+  prettier.pipe(split(function (line) {
+    t.ok(line.match(/\[.*\] INFO: hello world/), 'line does not match')
+
+    return line
+  }))
+
+  var instance = pino({ base: null }, prettier)
+
+  instance.info('hello world')
+})
+
+test('handles missing pid', function (t) {
+  t.plan(1)
+
+  var name = 'test'
+  var msg = 'hello world'
+  var regex = new RegExp('\\[.*\\] INFO \\(' + name + ' on ' + hostname + '\\): ' + msg)
+
+  var prettier = pretty()
+  prettier.pipe(split(function (line) {
+    t.ok(regex.test(line), 'line does not match')
+
+    return line
+  }))
+
+  var opts = {
+    base: {
+      name: name,
+      hostname: hostname
+    }
+  }
+  var instance = pino(opts, prettier)
+
+  instance.info(msg)
+})
+
+test('handles missing hostname', function (t) {
+  t.plan(1)
+
+  var name = 'test'
+  var msg = 'hello world'
+  var regex = new RegExp('\\[.*\\] INFO \\(' + name + '/' + process.pid + '\\): ' + msg)
+
+  var prettier = pretty()
+  prettier.pipe(split(function (line) {
+    t.ok(regex.test(line), 'line does not match')
+
+    return line
+  }))
+
+  var opts = {
+    base: {
+      name: name,
+      pid: process.pid
+    }
+  }
+  var instance = pino(opts, prettier)
+
+  instance.info(msg)
+})
+
+test('handles missing name', function (t) {
+  t.plan(1)
+
+  var msg = 'hello world'
+  var regex = new RegExp('\\[.*\\] INFO \\(' + process.pid + ' on ' + hostname + '\\): ' + msg)
+
+  var prettier = pretty()
+  prettier.pipe(split(function (line) {
+    t.ok(regex.test(line), 'line does not match')
+
+    return line
+  }))
+
+  var opts = {
+    base: {
+      hostname: hostname,
+      pid: process.pid
+    }
+  }
+  var instance = pino(opts, prettier)
+
+  instance.info(msg)
+})
+
 test('pino transform prettifies properties', function (t) {
   t.plan(1)
   var prettier = pretty()