Repository: pinojs/pino-pretty (javascript)

Issue:
prettifyMetadata does not log the 'caller' when the other properties are ignored
The function prettifyMetadata is responsible to log the caller of a log-statement.
But the conditional block that log the caller is guarded by a condition that requires either name, pid or hostname to be printed-out and forgetting to check for the presence of the property 'caller'.



Discussion:
Would you mind to send a PR to fix this?

Patch:
diff --git a/lib/utils.js b/lib/utils.js
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -215,7 +215,7 @@ function prettifyMessage ({ log, messageFormat, messageKey = MESSAGE_KEY, colori
 
 /**
  * Prettifies metadata that is usually present in a Pino log line. It looks for
- * fields `name`, `pid`, and `hostname` and returns a formatted string using
+ * fields `name`, `pid`, `hostname`, and `caller` and returns a formatted string using
  * the fields it finds.
  *
  * @param {object} input
@@ -226,8 +226,10 @@ function prettifyMessage ({ log, messageFormat, messageKey = MESSAGE_KEY, colori
  * returned. Otherwise, a string of prettified metadata is returned.
  */
 function prettifyMetadata ({ log }) {
+  let line = ''
+
   if (log.name || log.pid || log.hostname) {
-    let line = '('
+    line += '('
 
     if (log.name) {
       line += log.name
@@ -246,14 +248,17 @@ function prettifyMetadata ({ log }) {
     }
 
     line += ')'
+  }
 
-    if (log.caller) {
-      line += ` <${log.caller}>`
-    }
+  if (log.caller) {
+    line += `${line === '' ? '' : ' '}<${log.caller}>`
+  }
 
+  if (line === '') {
+    return undefined
+  } else {
     return line
   }
-  return undefined
 }
 
 /**


Test patch:
diff --git a/test/lib/utils.public.test.js b/test/lib/utils.public.test.js
--- a/test/lib/utils.public.test.js
+++ b/test/lib/utils.public.test.js
@@ -142,11 +142,46 @@ tap.test('prettifyMetadata', t => {
     t.is(str, '(1234 on bar)')
   })
 
-  t.test('works with all three present', async t => {
+  t.test('works with only `name`, `pid`, & `hostname` present', async t => {
     const str = prettifyMetadata({ log: { name: 'foo', pid: '1234', hostname: 'bar' } })
     t.is(str, '(foo/1234 on bar)')
   })
 
+  t.test('works with only `name` & `caller` present', async t => {
+    const str = prettifyMetadata({ log: { name: 'foo', caller: 'baz' } })
+    t.is(str, '(foo) <baz>')
+  })
+
+  t.test('works with only `pid` & `caller` present', async t => {
+    const str = prettifyMetadata({ log: { pid: '1234', caller: 'baz' } })
+    t.is(str, '(1234) <baz>')
+  })
+
+  t.test('works with only `hostname` & `caller` present', async t => {
+    const str = prettifyMetadata({ log: { hostname: 'bar', caller: 'baz' } })
+    t.is(str, '(on bar) <baz>')
+  })
+
+  t.test('works with only `name`, `pid`, & `caller` present', async t => {
+    const str = prettifyMetadata({ log: { name: 'foo', pid: '1234', caller: 'baz' } })
+    t.is(str, '(foo/1234) <baz>')
+  })
+
+  t.test('works with only `name`, `hostname`, & `caller` present', async t => {
+    const str = prettifyMetadata({ log: { name: 'foo', hostname: 'bar', caller: 'baz' } })
+    t.is(str, '(foo on bar) <baz>')
+  })
+
+  t.test('works with only `pid`, `hostname`, & `caller` present', async t => {
+    const str = prettifyMetadata({ log: { pid: '1234', hostname: 'bar', caller: 'baz' } })
+    t.is(str, '(1234 on bar) <baz>')
+  })
+
+  t.test('works with all four present', async t => {
+    const str = prettifyMetadata({ log: { name: 'foo', pid: '1234', hostname: 'bar', caller: 'baz' } })
+    t.is(str, '(foo/1234 on bar) <baz>')
+  })
+
   t.end()
 })