Skip to content

Commit

Permalink
Add asObjectBindingsOnly, allowing write to receive only the bindings…
Browse files Browse the repository at this point in the history
… as object

This allows to pass the format string and args as
is the underlying console when to keep their
special handling, for when using `write` to only
modify the default way the printing to the console
looks rather than writing to an alternate
destination entirely.

Part of #1556
  • Loading branch information
segevfiner committed Sep 26, 2024
1 parent 7cff45a commit 8a33834
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ function pino (opts) {
transmit,
serialize,
asObject: opts.browser.asObject,
asObjectBindingsOnly: opts.browser.asObjectBindingsOnly,
formatters: opts.browser.formatters,
levels,
timestamp: getTimeFunction(opts),
Expand Down Expand Up @@ -317,7 +318,7 @@ function createWrap (self, opts, rootLogger, level) {
argsIsSerialized = true
}
if (opts.asObject || opts.formatters) {
write.call(proto, asObject(this, level, args, ts, opts))
write.call(proto, ...asObject(this, level, args, ts, opts))
} else write.apply(proto, args)

if (opts.transmit) {
Expand Down Expand Up @@ -347,30 +348,43 @@ function asObject (logger, level, args, ts, opts) {
const argsCloned = args.slice()
let msg = argsCloned[0]
const logObject = {}
if (ts) {
logObject.time = ts
}

if (levelFormatter) {
const formattedLevel = levelFormatter(level, logger.levels.values[level])
Object.assign(logObject, formattedLevel)
} else {
logObject.level = logger.levels.values[level]
}

let lvl = (logger._childLevel | 0) + 1
if (lvl < 1) lvl = 1
// deliberate, catching objects, arrays
if (msg !== null && typeof msg === 'object') {
while (lvl-- && typeof argsCloned[0] === 'object') {
Object.assign(logObject, argsCloned.shift())

if (opts.asObjectBindingsOnly) {
if (msg !== null && typeof msg === 'object') {
while (lvl-- && typeof argsCloned[0] === 'object') {
Object.assign(logObject, argsCloned.shift())
}
}

const formattedLogObject = logObjectFormatter(logObject)
return [formattedLogObject, ...argsCloned]
} else {
if (ts) {
logObject.time = ts
}

if (levelFormatter) {
const formattedLevel = levelFormatter(level, logger.levels.values[level])
Object.assign(logObject, formattedLevel)
} else {
logObject.level = logger.levels.values[level]
}
msg = argsCloned.length ? format(argsCloned.shift(), argsCloned) : undefined
} else if (typeof msg === 'string') msg = format(argsCloned.shift(), argsCloned)
if (msg !== undefined) logObject[opts.messageKey] = msg

const formattedLogObject = logObjectFormatter(logObject)
return formattedLogObject
// deliberate, catching objects, arrays
if (msg !== null && typeof msg === 'object') {
while (lvl-- && typeof argsCloned[0] === 'object') {
Object.assign(logObject, argsCloned.shift())
}
msg = argsCloned.length ? format(argsCloned.shift(), argsCloned) : undefined
} else if (typeof msg === 'string') msg = format(argsCloned.shift(), argsCloned)
if (msg !== undefined) logObject[opts.messageKey] = msg

const formattedLogObject = logObjectFormatter(logObject)
return [formattedLogObject]
}
}

function applySerializers (args, serialize, serializers, stdErrSerialize) {
Expand Down

0 comments on commit 8a33834

Please sign in to comment.