Skip to content

Commit

Permalink
Merge pull request #882 from pinojs/revert-876-originalMsg
Browse files Browse the repository at this point in the history
Revert "Avoid data loss with conflicting 'msg' values."
  • Loading branch information
jsumners authored Aug 1, 2020
2 parents 3338013 + 88e6602 commit a4935cc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
8 changes: 4 additions & 4 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand Down
32 changes: 32 additions & 0 deletions docs/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"}
```
4 changes: 0 additions & 4 deletions lib/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 0 additions & 1 deletion test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ function levelTest (name, level) {
hostname,
level,
msg: 'string',
originalMsg: 'object',
hello: 'world'
})
})
Expand Down

0 comments on commit a4935cc

Please sign in to comment.