Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add asObjectBindingsOnly, allowing write to receive only the bindings as object #2051

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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