feat(browser): support nestedKey in asObject mode and browser.write - #2509
feat(browser): support nestedKey in asObject mode and browser.write#2509lx3133584 wants to merge 1 commit into
Conversation
Fixes pinojs#2138 Signed-off-by: Liang Xu <lx3133584@users.noreply.github.com>
kilisamemarisaaa
left a comment
There was a problem hiding this comment.
I found one correctness issue in the collision path. I reproduced it against this head with Node 24.12.0 and compared the emitted objects with the regular Node logger behavior; details are inline.
| while (lvl-- && typeof argsCloned[0] === 'object') { | ||
| Object.assign(logObject, argsCloned.shift()) | ||
| const target = (lvl === 0 && opts.nestedKey) | ||
| ? (logObject[opts.nestedKey] = logObject[opts.nestedKey] || {}) |
There was a problem hiding this comment.
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.
Problem
When using
pinoin browser / isomorphic environments withbrowser.asObject: true(or custombrowser.write), thenestedKeyconfiguration option was ignored. Logged payload objects were assigned directly to the top-level log object rather than nested undernestedKey, creating an inconsistency between Node.js and browser runtimes.Root Cause
setOptsinbrowser.jsdid not storenestedKey.asLogObjectunconditionally calledObject.assign(logObject, argsCloned.shift())for all object arguments instead of nesting payload objects underlogObject[opts.nestedKey]while keeping bindings at the top level.Fix
nestedKey: opts.nestedKey || opts.browser.nestedKey || nullinsetOpts.asLogObjectto assign payload objects (lvl === 0) tologObject[opts.nestedKey]while assigning binding objects (lvl > 0) to the top-levellogObject.Testing
test/browser.test.jsverifying thatnestedKeynests payload objects under the configured key in root and child loggers.