Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ function pino (opts) {
levels,
timestamp: getTimeFunction(opts),
messageKey: opts.messageKey || 'msg',
nestedKey: opts.nestedKey || opts.browser.nestedKey || null,
onChild: opts.onChild || noop,
redactFn
}
Expand Down Expand Up @@ -426,7 +427,10 @@ function asObject (logger, level, args, ts, opts) {
if (opts.asObjectBindingsOnly) {
if (msg !== null && typeof msg === 'object') {
while (lvl-- && typeof argsCloned[0] === 'object') {
Object.assign(logObject, argsCloned.shift())
const target = (lvl === 0 && opts.nestedKey)
? (logObject[opts.nestedKey] = logObject[opts.nestedKey] || {})
: logObject
Object.assign(target, argsCloned.shift())
}
}

Expand All @@ -439,7 +443,10 @@ function asObject (logger, level, args, ts, opts) {
// deliberate, catching objects, arrays
if (msg !== null && typeof msg === 'object') {
while (lvl-- && typeof argsCloned[0] === 'object') {
Object.assign(logObject, argsCloned.shift())
const target = (lvl === 0 && opts.nestedKey)
? (logObject[opts.nestedKey] = logObject[opts.nestedKey] || {})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reuses a same-named child binding as the payload container. On this head, child({ payload: 'binding' }).info({ foo: 'bar' }, 'hello') emits {"payload":"binding","msg":"hello"} in browser asObject mode, so the logged object is lost; the Node logger emits payload: { foo: "bar" }.

If the binding is an object, Object.assign mutates that shared binding instead: two successive logs with { first: 1 } and { second: 2 } make both already-collected records end up with payload: { binding: true, first: 1, second: 2 }.

Please create a fresh nested payload object for each log (overriding a colliding binding) rather than using ||, and add regression cases for primitive/object collisions plus two consecutive log calls.

: logObject
Object.assign(target, argsCloned.shift())
}
msg = argsCloned.length ? format(argsCloned.shift(), argsCloned) : undefined
} else if (typeof msg === 'string') msg = format(argsCloned.shift(), argsCloned)
Expand Down
36 changes: 36 additions & 0 deletions test/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,42 @@ test('opts.browser.asObject uses opts.messageKey in logs', ({ end, ok, is }) =>
end()
})

test('opts.nestedKey nests logged object payload under specified key in asObject mode', ({ end, is, same }) => {
const instance = require('../browser')({
nestedKey: 'payload',
browser: {
asObject: true,
write: function (o) {
is(o.level, 30)
is(o.msg, 'hello')
same(o.payload, { foo: 'bar', count: 42 })
}
}
})

instance.info({ foo: 'bar', count: 42 }, 'hello')
end()
})

test('opts.nestedKey with child loggers keeps bindings at top level and payload nested', ({ end, is, same }) => {
const instance = require('../browser')({
nestedKey: 'payload',
browser: {
asObject: true,
write: function (o) {
is(o.level, 30)
is(o.childKey, 'childVal')
is(o.msg, 'child message')
same(o.payload, { inner: 'data' })
}
}
})

const child = instance.child({ childKey: 'childVal' })
child.info({ inner: 'data' }, 'child message')
end()
})

test('opts.browser.asObjectBindingsOnly passes the bindings but keep the message unformatted', ({ end, ok, is, deepEqual }) => {
const messageKey = 'message'
const instance = require('../browser')({
Expand Down
Loading