Skip to content

Commit

Permalink
Merge pull request #795 from pinojs/issue-793
Browse files Browse the repository at this point in the history
Remove implicit appending of objects to message strings
  • Loading branch information
jsumners authored Mar 24, 2020
2 parents f867f31 + 33ab7fe commit fa32fe9
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 18 deletions.
5 changes: 2 additions & 3 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,8 @@ then be interpolated accordingly.
#### `...interpolationValues` (Any)

All arguments supplied after `message` are serialized and interpolated according
to any supplied printf-style placeholders (`%s`, `%d`, `%o`|`%O`|`%j`)
or else concatenated together with the `message` string to form the final
output `msg` value for the JSON log line.
to any supplied printf-style placeholders (`%s`, `%d`, `%o`|`%O`|`%j`) to form
the final output `msg` value for the JSON log line.

```js
logger.info('hello', 'world')
Expand Down
11 changes: 9 additions & 2 deletions lib/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function noop () {}
function genLog (z) {
return function LOG (o, ...n) {
if (typeof o === 'object') {
var msg = o
if (o !== null) {
if (o.method && o.headers && o.socket) {
o = mapHttpRequest(o)
Expand All @@ -38,8 +39,14 @@ function genLog (z) {
}
}
if (this[nestedKeySym]) o = { [this[nestedKeySym]]: o }
const formatParams = (o === null && n.length === 0) ? [null] : n
this[writeSym](o, format(null, formatParams, this[formatOptsSym]), z)
var formatParams
if (msg === null && n.length === 0) {
formatParams = [null]
} else {
msg = n.shift()
formatParams = n
}
this[writeSym](o, format(msg, formatParams, this[formatOptsSym]), z)
} else {
this[writeSym](null, format(o, n, this[formatOptsSym]), z)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"fast-safe-stringify": "^2.0.7",
"flatstr": "^1.0.12",
"pino-std-serializers": "^2.4.2",
"quick-format-unescaped": "^3.0.3",
"quick-format-unescaped": "^4.0.1",
"sonic-boom": "^1.0.0"
}
}
4 changes: 2 additions & 2 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function levelTest (name, level) {
instance.level = name

const sym = Symbol('foo')
instance[name]('hello', sym)
instance[name]('hello %s', sym)

const result = await once(stream, 'data')

Expand Down Expand Up @@ -628,7 +628,7 @@ test('fast-safe-stringify must be used when interpolating', async (t) => {

const o = { a: { b: {} } }
o.a.b.c = o.a.b
instance.info('test', o)
instance.info('test %j', o)

const { msg } = await once(stream, 'data')
t.is(msg, 'test {"a":{"b":{"c":"[Circular]"}}}')
Expand Down
10 changes: 5 additions & 5 deletions test/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ test('opts.browser.write func string joining', ({ end, ok, is }) => {
}
}
})
instance.info('test', 'test2', 'test3')
instance.info('test %s %s', 'test2', 'test3')

end()
})
Expand All @@ -207,7 +207,7 @@ test('opts.browser.write func string joining when asObject is true', ({ end, ok,
}
}
})
instance.info('test', 'test2', 'test3')
instance.info('test %s %s', 'test2', 'test3')

end()
})
Expand All @@ -223,7 +223,7 @@ test('opts.browser.write func string joining when asObject is true', ({ end, ok,
}
}
})
instance.info('test', 'test2', 'test3')
instance.info('test %s %s', 'test2', 'test3')

end()
})
Expand All @@ -238,7 +238,7 @@ test('opts.browser.write func string object joining', ({ end, ok, is }) => {
}
}
})
instance.info('test', { test: 'test2' }, { test: 'test3' })
instance.info('test %j %j', { test: 'test2' }, { test: 'test3' })

end()
})
Expand All @@ -254,7 +254,7 @@ test('opts.browser.write func string object joining when asObject is true', ({ e
}
}
})
instance.info('test', { test: 'test2' }, { test: 'test3' })
instance.info('test %j %j', { test: 'test2' }, { test: 'test3' })

end()
})
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/pretty/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ require('os').hostname = function () { return 'abcdefghijklmnopqr' }
var pino = require(require.resolve('./../../../'))
var log = pino({ prettyPrint: true })
log.error(new Error('kaboom'))
log.error(new Error('kaboom'), 'with', 'a', 'message')
log.error(new Error('kaboom'), 'with a message')
4 changes: 2 additions & 2 deletions test/levels.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ test('log null value when message is null', async ({ is }) => {
check(is, result, expected.level, expected.msg)
})

test('concatenate multiple params when base param is null', async ({ is }) => {
test('formats when base param is null', async ({ is }) => {
const expected = {
msg: 'a string',
level: 30
Expand All @@ -465,7 +465,7 @@ test('concatenate multiple params when base param is null', async ({ is }) => {
const stream = sink()
const instance = pino(stream)
instance.level = 'info'
instance.info(null, 'a', 'string')
instance.info(null, 'a %s', 'string')

const result = await once(stream, 'data')
check(is, result, expected.level, expected.msg)
Expand Down
4 changes: 2 additions & 2 deletions test/redact.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ test('redact option – interpolated object', async ({ is }) => {
const stream = sink()
const instance = pino({ redact: ['req.headers.cookie'] }, stream)

instance.info('test', {
instance.info('test %j', {
req: {
id: 7915,
method: 'GET',
Expand Down Expand Up @@ -180,7 +180,7 @@ test('redact.paths option – interpolated object', async ({ is }) => {
const stream = sink()
const instance = pino({ redact: { paths: ['req.headers.cookie'] } }, stream)

instance.info('test', {
instance.info('test %j', {
req: {
id: 7915,
method: 'GET',
Expand Down

0 comments on commit fa32fe9

Please sign in to comment.