Repository: pinojs/pino
Pull request #882 (2020-07-31 22:18:21)

Revert "Avoid data loss with conflicting 'msg' values."
Reverts pinojs/pino#876

This causes issues in dependent applications. See https://github.com/fastify/fastify/issues/2453

To get the same effect, use the log method hook:

```js
'use strict'

const log = require('pino')({
  level: 'debug',
  hooks: {
    logMethod (inputArgs, method) {
      if (inputArgs.length === 2 && inputArgs[0].msg) {
       inputArgs[0].originalMessage = inputArgs[0].msg
      }
      return method.apply(this, inputArgs)
    }
  }
})

log.info('no original message')
log.info({ msg: 'mapped to originalMsg' }, 'a message')

// {"level":30,"time":1596195825857,"pid":48580,"hostname":"mini2018","msg":"no original message"}
// {"level":30,"time":1596195825858,"pid":48580,"hostname":"mini2018","msg":"a message","originalMessage":"mapped to originalMsg","originalMsg":"mapped to originalMsg"}
```

cc: @markstos 


Patch:
diff --git a/docs/api.md b/docs/api.md
index 18e7424..2f99ec7 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -470,7 +470,7 @@ logger.info({MIX: {IN: true}})
 // {"level":30,"time":1531254555820,"pid":55956,"hostname":"x","MIX":{"IN":true}}
 ```
 
-<a id=message></a>
+<a id="message"></a>
 #### `message` (String)
 
 A `message` string can optionally be supplied as the first parameter, or
@@ -488,8 +488,8 @@ The `message` parameter takes precedence over the `mergedObject`.
 That is, if a `mergedObject` contains a `msg` property, and a `message` parameter
 is supplied in addition, the `msg` property in the output log will be the value of
 the `message` parameter not the value of the `msg` property on the `mergedObject`.
-In case this conflict occurs, the `msg` property from the object will be preserved
-in the key `originalMsg`.
+See [Avoid Message Conflict](./help.md#avoid-message-conflict) for information
+on how to overcome this limitation.
 
 The `messageKey` option can be used at instantiation time to change the namespace
 from `msg` to another string as preferred.
@@ -507,7 +507,7 @@ then be interpolated accordingly.
 * See [`messageKey` pino option](#opt-messagekey)
 * See [`...interpolationValues` log method parameter](#interpolationvalues)
 
-<a id=interpolationvalues></a>
+<a id="interpolationvalues"></a>
 #### `...interpolationValues` (Any)
 
 All arguments supplied after `message` are serialized and interpolated according
diff --git a/docs/help.md b/docs/help.md
index b9e392f..af8f457 100644
--- a/docs/help.md
+++ b/docs/help.md
@@ -12,6 +12,7 @@
 * [Pino with `debug`](#debug)
 * [Unicode and Windows terminal](#windows)
 * [Mapping Pino Log Levels to Google Cloud Logging (Stackdriver) Serverity Levels](#stackdriver)
+* [Avoid Message Conflict](#avoid-message-conflict)
 
 <a id="exit-logging"></a>
 ## Exit logging
@@ -282,3 +283,34 @@ module.exports = function createLogger(options) {
   return pino(Object.assign({}, options, defaultPinoConf))
 }
 ```
+
+<a id="avoid-message-conflict"></a>
+## Avoid Message Conflict
+
+As described in the [`message` documentation](./api.md#message), when a log
+is written like `log.info({ msg: 'a message' }, 'another message')` then the
+final output JSON will have `"msg":"another message"` and the `'a message'`
+string will be lost. To overcome this, the [`logMethod` hook](./api.md#logmethod)
+can be used:
+
+```js
+'use strict'
+
+const log = require('pino')({
+  level: 'debug',
+  hooks: {
+    logMethod (inputArgs, method) {
+      if (inputArgs.length === 2 && inputArgs[0].msg) {
+       inputArgs[0].originalMsg = inputArgs[0].msg
+      }
+      return method.apply(this, inputArgs)
+    }
+  }
+})
+
+log.info('no original message')
+log.info({ msg: 'mapped to originalMsg' }, 'a message')
+
+// {"level":30,"time":1596313323106,"pid":63739,"hostname":"foo","msg":"no original message"}
+// {"level":30,"time":1596313323107,"pid":63739,"hostname":"foo","msg":"a message","originalMsg":"mapped to originalMsg"}
+```
diff --git a/lib/tools.js b/lib/tools.js
index 00b7ab0..f45f898 100644
--- a/lib/tools.js
+++ b/lib/tools.js
@@ -109,10 +109,6 @@ function asJson (obj, msg, num, time) {
     obj = formatters.log(obj)
   }
   if (msg !== undefined) {
-    // If msg appears as a string and in the object, preserve it as "originalMsg"
-    if (obj[messageKey] !== undefined) {
-      obj.originalMsg = obj[messageKey]
-    }
     obj[messageKey] = msg
   }
   const wildcardStringifier = stringifiers[wildcardFirstSym]


Test patch:
diff --git a/test/basic.test.js b/test/basic.test.js
index 5f0ecd8..fa29c33 100644
--- a/test/basic.test.js
+++ b/test/basic.test.js
@@ -141,7 +141,6 @@ function levelTest (name, level) {
       hostname,
       level,
       msg: 'string',
-      originalMsg: 'object',
       hello: 'world'
     })
   })